Skip to content

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

Closed
@alan-agius4

Description

@alan-agius4

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.

Activity

added this to the Backlog milestone on Mar 17, 2023
Ozz760

Ozz760 commented on Mar 18, 2023

@Ozz760

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

alan-agius4

alan-agius4 commented on Mar 18, 2023

@alan-agius4
ContributorAuthor

Go for it

Ozz760

Ozz760 commented on Mar 21, 2023

@Ozz760

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

alan-agius4 commented on Mar 21, 2023

@alan-agius4
ContributorAuthor

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

Dev79844 commented on Apr 3, 2023

@Dev79844

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

alan-agius4

alan-agius4 commented on Apr 4, 2023

@alan-agius4
ContributorAuthor

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

Hazmi35

Hazmi35 commented on Apr 7, 2023

@Hazmi35

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

Anshul-target commented on Apr 14, 2023

@Anshul-target

@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

alan-agius4 commented on Apr 14, 2023

@alan-agius4
ContributorAuthor
moheet333

moheet333 commented on Apr 23, 2023

@moheet333

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

alan-agius4

alan-agius4 commented on Apr 23, 2023

@alan-agius4
ContributorAuthor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptGood First IssueWell scoped, documented and has the green lightHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      Participants

      @RyanCavanaugh@alan-agius4@Hazmi35@Dev79844@moheet333

      Issue actions

        Property is used before its initialization error is only shown during compilation when using ES2022 but is with ESNext Β· Issue #53286 Β· microsoft/TypeScript