7
1

More than 3 years have passed since last update.

「Announcing TypeScript 4.4 Beta」を読む

Posted at

が出ていたので、読んでみて個人的に気になったものをメモしようかと思います。


Control Flow Analysis of Aliased Conditions

function f(x: string | number | boolean) {
    const isString = typeof x === "string";
    const isNumber = typeof x === "number";
    const isStringOrNumber = isString || isNumber;
    if (isStringOrNumber) {
        x;  // Type of 'x' is 'string | number'.
    }
    else {
        x;  // Type of 'x' is 'boolean'.
    }
}

if文の外で型チェックしてもそれが引き継がれるようになる。素晴らしい、実際これに遭遇したことある気がする。

Note that there’s a cutoff – TypeScript doesn’t go arbitrarily deep when checking these conditions, but its analysis is deep enough for most checks.

なるほど。

Symbol and Template String Pattern Index Signatures

interface Colors {
    [sym: symbol]: number;
}

これを使うケースはなかったと思う。大抵はunique symbolを使うので。でも、これまで表現できなかったのができるようになったのは良い。

interface OptionsWithDataProps extends Options {
    // Permit any property starting with 'data-'.
    [optName: `data-${string}`]: unknown;
}

これは良い。stringにせざるを得なかったところが厳しくできる。

Defaulting to the unknown Type in Catch Variables (--useUnknownInCatchVariables)

TypeScript 4.0 allowed users to specify an explicit type annotation of unknown

すでにできてはいたのか。opt-inだけど。

This flag is enabled under the --strict family of options

おお、それはよい。既存コードでいくつかエラーになりそう。思い当たるところいくつかあり。

Exact Optional Property Types (--exactOptionalPropertyTypes)

interface Person {
    name: string,
    age?: number;
}

// With 'exactOptionalPropertyTypes' on:
const p: Person = {
    name: "Daniel",
    age: undefined, // Error! undefined isn't a number
};

なぜそういう型になっているのか不思議に思っていたものだ。直るのか。素晴らしい。

This flag is not part of the --strict family

あら、残念。

tsc --help Updates and Improvements

:raised_hands:

Performance Improvements

:rocket:

Spelling Suggestions for JavaScript

最近は、JSあまり使わないので関係ない。

Inlay Hints

VSCode使ってないので恩恵得られない。

まとめると

4.4にすると、catchはunknownになる。今から : unknown つけておけば早期発見できるかな。exactOptionalPropertyTypesは自分で有効にしないといけない。

7
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
1