LoginSignup
23
21

More than 5 years have passed since last update.

TypeScript 2.0.3 変更点

Posted at

こんばんは@vvakameです。

とかいいつつ、本当はRC変更点とかです。
ついに2系正式版リリースの運びとなり、大変めでたいです。
2.0 Beta変更点から実に2ヶ月半。長い。

変更点まとめ

薄味ですね。

次の2つはBetaからの要素でした。
タグ付きUnion型 Discriminated union types
tsconfig.jsonでのglobサポート support globs in tsconfig.json files property (or just file/directory exclusions)

Literal Typesの拡大

String Literal Typesの考え方がbooleanとnumber、Enumにも拡張されました。
これでJavaScriptのprimitiveっぽい型はLiteral Typesで書けるようになりました。

// 1桁の数字!
type Digit = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;

function pinNumber(a: Digit, b: Digit, c: Digit, d: Digit) {
}

// 1桁の数字×4 → OK!
pinNumber(1, 2, 3, 4);
// 2桁の数字が混ざってるのでコンパイルエラー
pinNumber(8, 7, 9, 10);

// JavaScript的に false な値の集合なども表現できる
type Falsy = "" | 0 | false | null | undefined;
let f: Falsy = 0;

// EnumもLiteral Types扱い
enum Suit {
    Heart, Spade, Club, Diamond,
}
type RedSuit = Suit.Heart | Suit.Diamond;
type BlackSuit = Suit.Spade | Suit.Club;

Playground

これだけなので見どころがない…!

23
21
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
23
21