コンテンツにスキップ

[TypeScript] 3.5

TypeScript3.5のリリース内容まとめ

Speed improvements

NOT NECESSARY EASY

型チェックとインクリメンタルビルドのスピードがアップした。
--incrementalのビルド時間は、v3.4と比べて最大68%削減されたらしい。

The Omit helper type

SHOULD EASY

型ヘルパーOmitが追加された。

type Classes = {
  a: string;
  b: string;
  c: string;
};

type OmitC = Omit<Classes, "c">
// Classesからプロパティcを抜いたものになる
// { a: string, b: string } と等価

Improved excess property checks in union types

HAD BETTER EASY

Union Typeの曖昧だった型チェックが強化された。

type Point = {
  x: number;
  y: number;
};

type Label = {
  name: string;
};

const thing: Point | Label = {
  x: 0,
  y: 9,
  // stringではなくbooleanでも通ってしまう
  name: true,
};
type Point = {
  x: number;
  y: number;
};

type Label = {
  name: string;
};

const thing: Point | Label = {
  x: 0,
  y: 9,
  // stringではなくbooleanだとエラーになる (string | undefinedならOK)
  name: true,
}

The --allowUmdGlobalAccess flag

UNKNOWN CAN NOT UNDERSTAND

--allowUmdGlobalAccessフラグをつけるとUMDのグローバル宣言を参照できるようになった。

Smarter union type checking

HAD BETTER EASY

union typeの判定がより正確になった。
v3.4まではエラーとなっていた以下のコードが通るようになった。

type WrapBool = { value: boolean; };
type WrapLikeBool = { value: true } | { value: false };

declare let bool: WrapBool;
declare let likeBool: WrapLikeBool;

// v3.4までだと falseにboolは代入できない..!! というエラーになる
// valueをbooleanとみなしてくれない
likeBool = bool;

以下のようにObjectでラップされていないUnion Typeは以前から問題ない。

type LikeBool = true | false;

declare let bool: boolean;
declare let likeBool: LikeBool;

likeBool = bool;

Higher order type inference from generic constructors

HAD BETTER EASY

コンストラクタ関数の場合でもHigher order type inference from generic functionsが有効になった。
ジェネリックな合成コンストラクタが作れたりする。

class Box1<T> {
  value: T;
  constructor(value: T) {
    this.value = value;
  }
}

class Box2<U> {
  value: U;
  constructor(value: U) {
    this.value = value;
  }
}

// T から U を作成するコンストラクタF
// U から V を作成するコンストラクタG
// を合成して T から V を作成するコンストラクタをつくる
function composeConstructor<T, U, V>(
  F: new (x: T) => U,
  G: new (y: U) => V
): (x: T) => V {
  return (x) => new G(new F(x));
}

// [v3.4] -> f: (x: {}) => Box2<{}>
// [v3.5] -> f: (x: {}) => Box2<Box1<T>>
let f = composeConstructor(Box1, Box2);