Skip to content

Incorrect "Property is used before its initialization" error with body-less arrow functions #45182

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jhnns opened this issue Jul 26, 2021 · 5 comments · Fixed by #45193
Closed
Labels
Bug A bug in TypeScript Help Wanted You can do this
Milestone

Comments

@jhnns
Copy link

jhnns commented Jul 26, 2021

Bug Report

🔎 Search Terms

Property is used before its initialization
arrow function
body
property
initializer
constructor parameter
useDefineForClassFields

🕗 Version & Regression Information

  • >= 3.8.3
  • Also nightly
  • This changed between versions 3.7.5 and 3.8.3

⏯ Playground Link

Playground link with relevant code

💻 Code

class A {
    value = 1;
}

class B {
    constructor(private a: A) {}

    // Shows "Property 'a' is used before its initialization"
    getAValue = () => this.a.value;
    
    // No error when there's a function body
    // getAValue = () => { return this.a.value };
}

useDefineForClassFields must be true

🙁 Actual behavior

With useDefineForClassFields: true there's an error when the arrow function has no body

🙂 Expected behavior

There should be no error

@akulaggarwal
Copy link

I upgraded to TS 4.7.4, problem persists.

@RyanCavanaugh
Copy link
Member

I upgraded to TS 4.7.4, problem persists.

Can you post an example? OP's code does not error in the 4.7 Playground

@Akimotorakiyu
Copy link

Akimotorakiyu commented Sep 12, 2022

image

@RyanCavanaugh please have a look! It's so confused!

It looks like has some trouble with compilerOptions.target in tsconfig.json, when change the target from ESNext to ES2022, this error disappear.

@MartinJohns
Copy link
Contributor

Target useDefineForClassFields Playground Error
ES2022 false link no (correct)
ES2022 true link no (wrong)
ESNext false link no (correct)
ESNext true link yes (correct)

@RyanCavanaugh On TypeScript 4.8.2, when having target set to ES2022 and enabled useDefineForClassFields there is no error, when there should be an error.

@chharvey
Copy link

chharvey commented Aug 7, 2023

Does this also address cases where the property name is used in a static field? In a static field, the object should be fully constructed so the property name should be accessible.

declare function assert(condition: boolean): asserts condition;

class Person {
    // Unexpected Error: Property 'name' is used before its initialization
    static initial_name_length = new Person().name.length;

    // fixed with an IIFE:
    static initial_name_length_fix = (() => new Person().name.length)();


    name = 'Alice';
}

assert(new Person().name === 'Alice');

assert(Person.initial_name_length === 5);

assert(Person.initial_name_length_fix === 5);

Playground

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Help Wanted You can do this
Projects
None yet
6 participants