Skip to content
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

Inform about useDefineForClassFields breaking change in TS >=4.3 #292

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Breaking-Changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,52 @@ enum E {

For more details, [see the original change](https://github.com/microsoft/TypeScript/pull/42472)

## Class fields while extending classes

TypeScript 4.3 introduces new `useDefineForClassFields` compiler option, which defaults to `true`.

The class' fields initialization will be included then.

```ts
class Example {
private abc: string;
}
```

Becomes:

```js
// TypeScript <4.3 or "useDefineForClassFields": false
class Example {
}

// TypeScript >=4.3 default behavior - "useDefineForClassFields": true
class Example {
abc;
}
```

It may introduce the problem when extending classes - after `super` constructor will be called, all child class properties will be reinitialized.
It may lead to unexpected behavior, where the value was set during super class constructor, but it's reset later.

```ts
class Parent {
public value: string | null;

public constructor() {
this.value = "test value";
}
}

class Child extends Parent {
public value!: string;
}

console.log(new Child().value);
// for `useDefineForClassFields: false` -> "test value"
// for `useDefineForClassFields: true` -> undefined
```

# TypeScript 4.2

## `noImplicitAny` Errors Apply to Loose `yield` Expressions
Expand Down