Skip to content

Property initializers output for non-initialized types #45076

Open
@nonara

Description

@nonara

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:

class A {
  a?: string
}

Output is:

// target = ESNext
class A {
  a; // Outputs a statement without initialized value, which is treated as a = undefined;
}

// target = ES2020
class A {
}

Here is a simplified version of how this affected me

abstract class Base<T extends Record<string, any>> {
  // Single constructor for all derivatives
  constructor(o: Omit<T, typeof Base>) {
    Object.assign(this, o);
  }
}

class A extends Base<A> {
  myProp?: string
  myProp2!: string
}

const a = new A({ myProp2: 'hello' });

// ESNext: a = { myProp2: undefined, myProp: undefined }
// ES2020: a = { myProp2: 'hello' }

The above produces:

// TypeScript target=ESNext
class A extends Base {
  myProp;
  myProp2; // Note the boomed property still gets output here, so it is initialized after the super call - which maybe surprising behaviour
}

// ES2020 / Babel
class A extends Base {
}

🔎 Search Terms

  • property initializers

🕗 Version & Regression Information

TS 4.3.5

Solution

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

ie:

class A extends Base<A> {
  declare myProp?: string
  declare myProp2: 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