Skip to content

Incorrect error 2301 when using ES standard class properties #49719

@sandersn

Description

@sandersn

As explained in #2548, the following code is not correct when useDefineForClassFields: false, the default for targets before ES2021, or when target <= ES2020.

const x = 1
class C {
    p = x
    constructor(x: string) { }
}

However, when useDefineForClassFields: true and target >= ES2021, there shouldn't be an error here.

Expected: No error 2301 on private p = x, and p: number
Actual: Error on private p = x and p: any

Edit: @fatcerberus pointed out that the error is only incorrect when useDefineForClassFields: true and target >= ES2021, because downlevel define for class fields still happens in the constructor.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptGood First IssueWell scoped, documented and has the green light

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions