TypeScript 5.5 Beta のアナウンスが出たので使いそうな構文周りをざっくり確認した
Inferred Type Predicates
以下でbirdsがundefined
ではなく、birdsがBird[]
に推論されるようになった
function makeBirdCalls(countries: string[]) { // birds: (Bird | undefined)[] const birds = countries .map(country => nationalBirds.get(country)) .filter(bird => bird !== undefined); for (const bird of birds) { bird.sing(); // Now okay, previously was error } }
filter通した後でもundefinedのチェックをしなくてはならなかったので扱いやすくなった
Control Flow Narrowing for Constant Indexed Accesses
以下がエラーにならなくなった
function f1(obj: Record<string, unknown>, key: string) { if (typeof obj[key] === "string") { // Now okay, previously was error obj[key].toUpperCase(); } }
Regular Expression Syntax Checking
明らかな正規表現の誤りを検知してくれるようになった
The ${configDir} Template Variable for Configuration Files
モノレポだとtsconfig.jsonが複数できるが、その場合相対パスで読み先を指定していたりする
これだと分かりづらいので、${configDir}
という変数を使って${configDir}
からのパスでかけるようになった
{ "compilerOptions": { "typeRoots": [ "${configDir}/node_modules/@types" "${configDir}/custom-types" ], "outDir": "${configDir}/dist" } }