とーますメモ

Ruby on Rails / Goなどの学習メモ

TypeScript 4.5 ファーストインプレッション

個人用メモ。解釈が正しいかは不明なので参考までに。

TypeScript4.5がリリースされたので、その内容を簡単に試してみる。
devblogs.microsoft.com

TypeScriptを既にインストールしている場合は、以下で最新版に更新。

$ npm update typescript

初めてインストールする場合は以下。

$ npm install typescript

公式によるとTypeScript4.5の主な追加項目は以下。

The Awaited Type and Promise Improvements

The Awaited type can be helpful for modeling existing APIs, including JavaScript built-ins like Promise.all, Promise.race, etc. In fact, some of the problems around inference with Promise.all served as motivations for Awaited.

https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#awaited-type

よくわからん。何が便利になるのかも不明。

=> PromiseLikeの説明
thenが実装しているPromise風オブジェクトのこと?

const fakePromise = { then: () => string };

async/await がやっていること - Qiita
JavaScriptの非同期処理をじっくり理解する (2) Promise

Supporting lib from node_modules

TypeScript does bundle certain libraries to ensure it works together well with Javascript. However, you might want to change those to another type of library implementation.

It is a similar behavior as of @types where you can now choose your appropriate type definitions.

https://betterprogramming.pub/whats-new-in-typescript-4-5-57d6b88b1e72

TypeScriptにバンドルされているライブラリの型定義ファイルを
指定することができるようになるため、適切な型定義ファイルを使用できるようになる?みたい。

TypeScript 4.5 変更点 まとめ

Template String Types as Discriminants

https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#template-string-discriminants

以下のような、テンプレートリテラルの使い方ができるようになった。4.4以下では失敗する。

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

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

export function handler(r: Success | Error) {
    if (r.type === "HttpSuccess") {
        // 'r' has type 'Success'
        let token = r.body;
    }
}

--module es2022

https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#module-es2022

TypeScriptでes2022モジュールをサポートできるようになった。
一番重要だと思われる機能は、awaitをasync関数外でも使用できるようになること。

Tail-Recursion Elimination on Conditional Types

Conditional Typesで型レベル末尾再帰最適化を行うらしい。

TypeScript 4.5 変更点 まとめ

よくわからん。飛ばす...

Disabling Import Elision

特定の条件下の元で、importしたモジュールを使用している場合、TypeScriptはそのモジュールを未定義と判断し削除するのを防ぐオプション。

以下のようなケースが想定される。

import { Animal } from "./animal.js";
eval("console.log(new Animal().isDangerous())");

Announcing TypeScript 4.5 - TypeScript

import { readFile } from "node:fs/promises";
eval(`readFile("./package.json", "utf-8").then(console.log);`);

TypeScript 4.5 変更点 まとめ

type Modifiers on Import Names

今まではtype修飾子をimport文で使用するときは、モジュールのimportととは分けて記述する必要があったが、4.5からは一緒にかけるようになった。

import type { FC } from 'react';
import { useEffect } from 'react';
import { type FC, useEffect } from 'react';

Private Field Presence Checks

プライベートプロパティの存在チェック。ユースケースがよくわからないが、
このチェックを使用することで、あるインスタンスが、どのクラスから生成されたかを証明することができるようになるということだと思う。
TypeScript 4.5 変更点 まとめ

Import Assertions

ランタイム実行時に、インポートする対象が想定したフォーマットになっているかチェックすることができる。この機能は、どうやらJSONなどのコードを実行する機能がないファイルをインポートする場合は、明示的にフォーマットを指定させることでセキュリティを向上させる狙いがあるっぽい。
GitHub - tc39/proposal-import-assertions: Proposal for syntax to import ES modules with assertions

Const Assertions and Default Type Arguments in JSDoc

一例として、as constを使用せずにJSDocのアサーションを使用することで同じ表現が可能になった?的なことかと。

Faster Load Time with realPathSync.native

内部的な話っぽいので、スキップ。

New Snippet Completions

補完機能が強化された模様。読む限り良さげな改善内容。
新しいVSCode上で動作するとのことだが、自分の環境では確認できず...

Better Editor Support for Unresolved Types

特定のケースにおいて、関数名をマウスオーバー時に表示されるコンテキスト情報が改善された模様。

Experimental Nightly-Only ECMAScript Module Support in Node.js

実験的な機能についての言及。よくわからない。


[参考資料]
TypeScript 4.5 変更点 まとめ
What’s New in TypeScript 4.5?. Awaited Type, type-only modifier… | by Jose Granja | Nov, 2021 | Better Programming