TypeScript Concepts

Control-flow analysis — flow-sensitive types

The checker tracks possible types along reachable paths using tests, assignments and control-flow joins. A guard may narrow a value inside a branch; after an assignment the known type can change. Narrowing is not a runtime check inserted by the compiler, nor a complete analysis of every mutation through aliases or callbacks. Observed output: '3 8'. Local example verified with TypeScript 5.8.3, strict checking, ES2022/CommonJS and Node.js 22.16.0; declarations enabled.

Formal Syntax & Structure

typescript syntaxtypescript
if (typeof value === "string") { /* narrowed */ }

Working Example

Example Snippettypescript
function size(value: string | number): number {  if (typeof value === "string") return value.length;  return value * 2;}console.log(size("cat"), size(4));
TypeScript Reference