TypeScript v4.6
2022/2/28
継承しているclassの constructor()
内で、 super()
より前に、propertyに無関係の処理を書いていてもerrorにしなくなった
これ、v4.5以前ではダメだったんだ

これはv4.6でもerrorになる
tsclass Derived extends Base {
someProperty = true;
constructor() {
this.someProperty = false
super();
}
}
super()
の前でpropertyを初期化している
JSのclassとの兼ね合いで、これを防ぎたかった
が、4.5以前では制限が厳しすぎて、 super()
以前に何を書いてもerrorにしていた
それが適切になるように修正された
PR出してから3年間放置されていたので、皮肉でそれを祝したケーキをTwitterに上げた
それがきっかけか知らんけど、直後にmergeされた
objectを分割代入したあとも、Control Flow Analysisが効く
tstype Action =
| { kind: "NumberContents", payload: number }
| { kind: "StringContents", payload: string };
function processAction(action: Action) {
const { kind, payload } = action;
if (kind === "NumberContents") {
let num = payload * 2
// ...
}
else if (kind === "StringContents") {
const str = payload.trim();
// ...
}
}
この記事が詳しい
Improved Recursion Depth Checks
こっちも読まないと理解できないと思う

v4.5.3で既に入っている機能らしい
定義上で再帰した場合と、手動で再帰した場合とで挙動が異なるようになった
定義上で再帰した場合、再帰するたびにtype IDが増えていく
type IDが増えていき、再帰回数が3つ以上ある場合、「同じものだ」と判断する
以前はこの回数が5回だった
同じものだとみなされた場合は、人間が見て明らかに異なる場合でもエラーが出ない
コレ自体は、4.6前後で変わっていない
変わったのは、手動で再帰した場合に「再帰するたびにtype IDが増えていく」というのをしなくなった
そのため、3回以上再帰していても、末端まで見て評価されるのでより健全になる
手動で再帰しているもの
tsinterface Foo<T> {
prop: T;
}
declare let x: Foo<Foo<Foo<Foo<Foo<Foo<string>>>>>>;
declare let y: Foo<Foo<Foo<Foo<Foo<string>>>>>;
x = y; // v4.5.3より前ではok. v4.6からerror
5回再帰している
そのため、v4.6(v4.5.3)より前では、「無限なので同じだ」と判断されてエラーが出なかった
それ以降からは、ちゃんとエラーが出るようになった
定義上で再帰しているもの
tsinterface Source<T> {
prop: Source<Source<T>>;
}
interface Target<T> {
prop: Target<Target<T>>;
}
function check(source: Source<string>, target: Target<number>) {
target = source;
}
人間が見ればエラーになってほしい感じがするが、これは4.5以前同様エラーにならない
4.6で変わったのは、「無限だ」と判断するための再帰回数が5回から3回になった点
これによりパフォーマンスが改善された
Control Flow Analysis for Dependent Parameters
可変長引数 ...args
に対するType Guardの改善
tstype Func = (...args: ["a", number] | ["b", string]) => void;
const f1: Func = (kind, payload) => {
if (kind === "a") {
// 以前 payload :: number | string
// v4.6 payload :: number
}
if (kind === "b") {
// 以前 payload :: number | string
// v4.6 payload :: string
}
};
Removed Unnecessary Arguments in react-jsx
jst: react-jsx
の時にコンパイル結果に不要なものが含まれていたので除去した
Indexed Access Inference Improvements
PRを読むのが面倒くさい

release noteの方を見れば改善されたことはわかるが、具体的なユースケースに落とし込んで理解したい
たぶんPRに書いてる
--target es2022
JSDoc Name Suggestions
More Syntax and Binding Errors in JavaScript
TypeScript Trace Analyzer
Breaking Changes