Lesson 03 of 55
8 mins readPython Type Inference & Type Widening
In Plain English
TypeScript can automatically infer types without explicit annotations. Variables declared with `let` infer widened types (like `string`), while `const` infers narrow literal types.
Deep Dive: How It Works
Type Inference: TypeScript automatically determines the type when a variable is initialized (`let x = 10` is inferred as `number`).
Const Widening: `const mode = "dark"` infers the literal type `"dark"`; `let mode = "dark"` infers `string`.
Contextual Typing: Parameter types are inferred from caller context (e.g. inside `array.map(x => ...)`).
Core Rules to Remember

Omit Redundant Annotations: Writing `let x = 10` is cleaner than `let x: number = 10`.

Contextual Inferences: Higher-order callbacks infer argument types from the parent array/function.
Live Interactive Example
Hit Run Code to see it liveType Inference and Contextual Typing
Python 3.12
1
2
3
4
5
6
7
Output Console
Click "Run Code" to view the rendered output.
How it works: TypeScript automatically inferred the array type and the map callback argument type.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Use Contextual Inference in Array Filter
Declare `const scores = [65, 88, 92, 45, 99];`
Filter scores `>= 80` using `scores.filter(s => s >= 80)` without explicit type annotations.
Log `"Honor scores: " + passing.join(", ")`.
1
2
3
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.