TypeScript 4.5をさらってみる

  • 2021年12月7日
  • 2021年12月7日
  • TypeScript
  • 357view

TypeScript4.5がリリースされたので、追加点を確認しておく。

「Awaited」型の導入

Awaited型はawait,async関数の操作をモデル化する目的で使う。

例)

// A = string
type A = Awaited<Promise<string>>;

// B = number
type B = Awaited<Promise<Promise<number>>>;

// C = boolean | number
type C = Awaited<boolean | Promise<number>>;

node_modules から lib を追加可能に

libを用いることで、標準ライブラリの差し替えが楽になる。

TypeScript 4.5でますます便利に! better-typescript-lib v2

文字列リテラル型の代わりにテンプレートリテラル型を使うことができるようになった

export interface Success {
    type: `${string}Success`;
    body: string;
}

export interface Error {
    type: `${string}Error`;
    message: string;
}

export function handler(r: Success | Error) {
    // Successで終わる文字列型
    if (r.type === "HttpSuccess") {
        // rはSuccess型
        let token = r.body;
    }
}

まだ用途が定まらず理解が難しい。

–module es2022 の追加

es2022がコンパイルオプションとして使えるようになったらしい。 es2022の仕様がまず分かっていないからキャッチアップかなあ。。

参考記事
TypeScript 4.5 変更点 まとめ

その他、諸々あるけどよく分らん。

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
  • Attributes
  • Custom attributes
  • Custom fields
Click outside to hide the compare bar
Compare
Compare ×
Let's Compare! Continue shopping