TypeScript v4.4
2021/8/12
RC
Control Flow Analysis of Aliased Conditions
TypeScriptのControl Flow Analysisの改善
いったん定数に入れた後にも分岐できるようになった
code:ts
type Shape =
| { kind: 'circle'; radius: number }
| { kind: 'square'; sideLength: number };
function area(shape: Shape): number {
const { kind } = shape;
if (kind === 'circle') {
return Math.PI * shape.radius ** 2;
}
return shape.sideLength ** 2;
}
従来は、if (shape.kind === 'circle')のように書かないとダメだったmrsekut.icon
条件式を変数に入れて、使うタイミングをずらしてもguardできる
code:ts
function f(x: string | number | boolean) {
const isString = typeof x === 'string';
const isNumber = typeof x === 'number';
const isStringOrNumber = isString || isNumber;
if (isStringOrNumber) {
x; // 'string | number'.
} else {
x; // 'x' is 'boolean'.
}
}
従来は、if (typeof x === 'string' || typeof x === 'number')と書かないとダメだったmrsekut.icon
他にも色々
コード例が良いねmrsekut.icon
Symbol and Template String Pattern Index Signatures
index signatureの改善
keyにsymbolとTemplate Literal Typesを使えるようになった
code:ts
interface Colors {
sym: symbol: number;
}
code:ts
interface OptionsWithDataProps extends Options {
[optName: data-${string}]: unknown;
}
useUnknownInCatchVariablesの導入
catch (err)のerrが、もともとanyだったがunknownになる
exactOptionalPropertyTypesの導入
recordのoptional?: ..なpropertyに、undefinedを代入することを禁止する
Class Static Blocks
classにstaticblockを導入
code:ts
class Foo {
static Foo.count = 0;
static {
if (someCondition()) {
Foo.count++;
}
}
}
TypeScriptでは基本的にclassを使わないのであまり興味がないmrsekut.icon
というかこれ、ECMAScriptに入っているんだmrsekut.icon
Spelling Suggestions for JavaScript
@ts-checkを使ったときのJSのスペルチェック
Inlay Hints
https://devblogs.microsoft.com/typescript/announcing-typescript-4-4-rc/#inlay-hints
Auto-Imports Show True Paths in Completion Lists
補完するときのpathが見やすくなった
色々速くなった