From 11e5c714348f3c47476c23b0c1e0b6a2f2f75369 Mon Sep 17 00:00:00 2001 From: Dawid Rusnak Date: Wed, 9 Feb 2022 13:26:49 +0100 Subject: [PATCH] Inform about `useDefineForClassFields` breaking change in TS >=4.3 --- Breaking-Changes.md | 46 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Breaking-Changes.md b/Breaking-Changes.md index 65e03910..45dcd4f6 100644 --- a/Breaking-Changes.md +++ b/Breaking-Changes.md @@ -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