Skip to content

Property initializers output for non-initialized types #45076

Description

Bug Report

This is technically not a bug, given ES Spec, however, I'm filing an issue in case an consideration needs to be made on how to handle with regard to documentation, etc, and/or to provide a solution for people facing the same.

Issue Summary

  • ESNext spec seems to call for defining all class fields. This also affects those which are optional or specified with a boom.
  • Spec also dictates that property initializers are called after the super call. (https://github.com/tc39/proposal-class-fields)

This results in any properties specified in the inherited class that are assigned during the base class constructor to be overwritten, even if they do not have initializer values specified in the inherited class.

The behaviour which causes the error can be seen below:

classA{a?: string}

Output is:

// target = ESNextclassA{a;// Outputs a statement without initialized value, which is treated as a = undefined;}// target = ES2020classA{}

Here is a simplified version of how this affected me

abstractclassBase<TextendsRecord<string,any>>{// Single constructor for all derivativesconstructor(o: Omit<T,typeofBase>){Object.assign(this,o);}}classAextendsBase<A>{myProp?: stringmyProp2!: string}consta=newA({myProp2: 'hello'});// ESNext: a = { myProp2: undefined, myProp: undefined }// ES2020: a = { myProp2: 'hello' }

The above produces:

// TypeScript target=ESNextclassAextendsBase{myProp;myProp2;// Note the boomed property still gets output here, so it is initialized after the super call - which maybe surprising behaviour}// ES2020 / BabelclassAextendsBase{}

🔎 Search Terms

  • property initializers

🕗 Version & Regression Information

TS 4.3.5

Solution

For those facing this issue, simply change property declarations to ambient.

ie:

classAextendsBase<A>{declaremyProp?: stringdeclaremyProp2: string}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsThe issue relates to how you learn TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions