動くコード図鑑技術記事現場の渡り方キャリア論すべての記事About
TypeScript

対応 6: Aggregate ↔ reduce (型推論の罠)

出典: C# LINQ → TypeScript Array methods 翻訳早見表 — Where/Select/GroupBy が filter/map/reduce にどう写るか対応 6: Aggregate ↔ reduce (型推論の罠)

対応 6: Aggregate ↔ reduce (型推論の罠) (typescript)#33e1420c284a
// TypeScript: reduce は seed なしだと累積型推論で詰まる
const total = products.reduce((acc, p) => acc + p.price, 0);
// 累積が配列やオブジェクトの場合は明示型が必要
const grouped = products.reduce<Record<string, number>>((acc, p) => {
    acc[p.category] = (acc[p.category] ?? 0) + p.price;
    return acc;
}, {});
▸ この snippet は実行結果未収録
▸ 実行結果は未収録です
  • id: #33e1420c284a
  • lines: 7
  • extracted: 2026-06-10

Source収録記事

この snippet は記事の「対応 6: Aggregate ↔ reduce (型推論の罠)」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。

同じ記事から

8
図鑑トップ