Skip to content

Property is used before its initialization error is only shown during compilation when using ES2022 but is with ESNext #53286

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
alan-agius4 opened this issue Mar 16, 2023 · 11 comments Β· Fixed by #53885
Labels
Bug A bug in TypeScript Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Milestone

Comments

@alan-agius4
Copy link
Contributor

alan-agius4 commented Mar 16, 2023

Bug Report

πŸ”Ž Search Terms

  • property is used before its initialization
  • esnext
  • es2022
  • before its initialization

πŸ•— Version & Regression Information

  • Does not seem to be a regression.

⏯ Playground Link

ESNext: https://www.typescriptlang.org/play?useDefineForClassFields=true&target=99&ts=5.1.0-dev.20230315&ssl=4&ssc=2&pln=1&pc=1#code/MYGwhgzhAEBiD29oG9oChqegIzAJ2gF5oAXACwEsIA6bAVwC8BuDLYeAOwhLzuBPh4AFAAc8FAG5gSAUxyMAXNDocA1h3gB3DgEoUAXzT6gA

ES2022: https://www.typescriptlang.org/play?useDefineForClassFields=true&target=9&ts=5.1.0-dev.20230315&ssl=4&ssc=2&pln=1&pc=1#code/MYGwhgzhAEBiD29oG9oChqegIzAJ2gF5oAXACwEsIA6bAVwC8BuDLYeAOwhLzuBPh4AFAAc8FAG5gSAUxyMAXNDocA1h3gB3DgEoUAXzT6gA

πŸ’» Code

class Foo { 
    bar = this.buz;
    constructor(private buz: unknown) {}
}

πŸ™ Actual behavior

TSC correctly produces the below compilation error when targeting ESNext. But does not when targeting ES2022.

TS2729: Property 'buz' is used before its initialization.

πŸ™‚ Expected behavior

The same compilation error is shown when targetting ES2022.

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Help Wanted You can do this Good First Issue Well scoped, documented and has the green light labels Mar 17, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 17, 2023
@Ozz760
Copy link

Ozz760 commented Mar 18, 2023

Hello Alan,
I was wondering if I can take this issue?

@alan-agius4
Copy link
Contributor Author

Go for it

@Ozz760
Copy link

Ozz760 commented Mar 21, 2023

Hello,
While I was researching this issue there were similar issue's that came up.
#45325
#45182
I'm not sure if this is consider as a duplicate.
Please let me know.
Thank you.

@alan-agius4
Copy link
Contributor Author

It’s not a duplicate as those issues are closed and problem still persists.

It appears that the problem was only fixed for esnext.

@Dev79844
Copy link

Dev79844 commented Apr 3, 2023

Hey Alan!
I would like to work on this issue if anyone is not working on it.

@alan-agius4
Copy link
Contributor Author

@Dev79844, no one has opened a PR so far so feel free to open one.

@Hazmi35
Copy link

Hazmi35 commented Apr 7, 2023

Can confirmed this issue is happening with 5.0.4. Maybe related #50971 and #52331

ES2022 (and above, including ESNext) implies useDefineForClassFields: true. TS2729 will happen if this option is set to true.

On ESNext, if you set the option to true, TS2729 shows up, and when false it doesn't. This is expected.

But on ES2022, even with useDefineForClassFields: true explicitly set to true, there are no TS2729 errors.

@Anshul-target
Copy link

@alan-agius4 I am new to opensource could you suggest how would I test the error.
I have set up the project locally

@alan-agius4
Copy link
Contributor Author

@Anshul-target, please see the contributing guidelines.

@moheet333
Copy link

Hey @alan-agius4 does the PR #53885 fix this issue?

@alan-agius4
Copy link
Contributor Author

Hey @alan-agius4 does the PR #53885 fix this issue?

Looks like it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Good First Issue Well scoped, documented and has the green light Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants