cd ../labs
TypeScriptintermediateabout 40 minutes
TypeScript: types that catch bugs
Annotations, inference, unions and narrowing, read the way the compiler reads them, then write typed code that runs.
By the end you can
- Say what a value is allowed to be
- Read what the compiler infers on its own
- Handle a union by narrowing it
- Type an object and a function signature
Easier if you did first
0 of 3 done
step 1 of 3
What the compiler sees
TypeScript is JavaScript plus types. The types are checked before your code runs and then erased: nothing of them exists at runtime.
Most of the time you do not write types at all, because they are inferred. Knowing what gets inferred is most of the skill.
These questions are about reading types rather than writing them, which is what you do all day in a typed codebase.
question 1 of 4
What type is inferred for total?
const prices = [360, 600, 1000]; const total = prices.reduce((sum, p) => sum + p, 0);question 2 of 4
Why does this fail to compile?
function greet(name: string) { return "Hello " + name; } greet(undefined);question 3 of 4
What is wrong here?
type Plan = { name: string; price: number }; const plan: Plan = { name: "Starter", price: "600", };question 4 of 4
What does this union mean for how you use result?
function find(id: string): Plan | null { // ... } const result = find("abc"); console.log(result.name);