Problem Statement
Demonstrate explicit typing vs inference and when to prefer each.
Explanation
Type inference keeps local code concise: `const count = 0` becomes number automatically. Explicit annotations document intent and lock APIs: annotate parameters, return types, and exported values. Use inference for locals; annotate module boundaries or whenever inference would be too wide or unclear.
Code Solution
SolutionRead Only
// Inferred local
const answer = 42; // number
// Annotated API
export function add(a: number, b: number): number { return a + b }
// Too-wide inference without const-assertion
const colors = ['red', 'green']; // string[]
const tagged = ['ok', 200] as const; // readonly ['ok', 200]
// Annotate when needed
let id: string | null = null;