Azlaan Electric AC Solar Center — Solar, CCTV, AC & electrical work, Faisalabad Call: 0300-8987448

TypeScript Type Inference Playground

Paste a TypeScript snippet and see how the TypeScript compiler would infer the type of every variable. This rule-based engine mirrors the real inference rules: literal widening, let versus const, unions from repeated assignments, array element unions, ternary branches, typeof narrowing and function return types.

Try a preset
Inferred types
VariableDeclaredInferred typeNotes

How it works

  • Literal widening: let s = "hi" widens to string, while const s = "hi" keeps the literal type "hi".
  • Unions: every assignment to the same variable adds to its type, so let x = 1; x = "a" becomes string | number.
  • Arrays: element types are merged into a union, for example (string | number | boolean)[].
  • Ternary: cond ? 1 : "x" infers string | number from the two branches.
  • Narrowing: a typeof x === "string" guard narrows x to string inside the block.
  • Functions: return statements are scanned and their expression types are merged into an inferred return type.

This is a simplified rule engine for learning purposes. The real TypeScript compiler also considers control flow, generics, context and declaration merging.