Upgrade to Pro — share decks privately, control downloads, hide ads and more …

鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力

 鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力

2026年06月30日(火)に開催された、Findy「鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力」で発表した資料です。

https://findy.connpass.com/event/396860/

Avatar for tonkotsuboy_com

tonkotsuboy_com

June 29, 2026

More Decks by tonkotsuboy_com

Other Decks in Programming

Transcript

  1. const nums = [10, 2, 30]; nums.sort((a, b) => a

    - b); console.log(nums); // [2, 10, 30] sort() は元の配列を壊す
  2. const nums = [10, 2, 30]; const sorted = nums.toSorted((a,

    b) => a - b); sorted; // [2, 10, 30] nums; // [10, 2, 30] ← 元はそのまま toSorted() なら非破壊 Playgroundで動かす
  3. :value is number という「型述語」を書かないと number[] にならない const result = [12,

    null, 24, undefined, 48] .filter( (value): value is number => value != null, ); 配列のnull を除外するケース
  4. const result = [12, null, 24, undefined, 48] .filter( (value)

    => value != null, ); result; // number[] ← 自動で推論される 5.5 から自動で推論される レシピ115(第9章)   Playgroundで動かす
  5. 指定したインデックスの最初の文字(at(0) )を取得する foods[0] ・foods[1] ・foods[2] はstring と推論される foods[2] は実際は存在しない(undefined )なのでエラー

    const foods: string[] = ["カレー", "うどん"]; console.log(foods[0].at(0)); // 「カ」 console.log(foods[1].at(0)); // 「う」 console.log(foods[2].at(0)); // ランタイムエラー 文字列型の配列
  6. ?. で要素の存在チェックをしないとエラーになる foods[0] など明らかに存在する要素もチェックが必要 const foods: string[] = ["カレー", "うどん"];

    console.log(foods[0]?.at(0)); // 「カ」 console.log(foods[1]?.at(0)); // 「う」 console.log(foods[2]?.at(0)); // undefined noUncheckedIndexedAccess が有効なとき Playgroundで動かす