Inference, annotations and the unknown family
Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.
What you will be able to do
#- Predict when TypeScript infers a literal type and when it widens.
- Choose between any, unknown and never deliberately.
- Annotate function boundaries while letting the body infer.
Overview
#Inference first
TypeScript infers a type when you do not annotate. const keeps the narrow literal; let widens so the variable can be reassigned.
const a = "ready"; // type "ready"
let b = "ready"; // type string
Annotate at boundaries (function parameters and exported return types); let the body infer.
The three escape hatches
anyturns checking off for that value and spreads silently. Avoid it.unknownaccepts any value but forces you to narrow before use. Prefer it at untrusted boundaries.neveris the type with no values. It appears in exhaustiveness checks and functions that never return.
function fail(message: string): never {
throw new Error(message);
}
strictNullChecks
With strict mode, null and undefined are not part of every type. A parameter typed string cannot receive null unless you write string | null.
Widening and literal types
Object and array literals widen their members unless you use as const. That is why const config = { mode: "dark" } gives mode: string, while const config = { mode: "dark" } as const gives mode: "dark".
Rule of thumb
Annotate what a function promises to callers. Infer everything else.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What type does TypeScript infer for each variable? ```ts const a = "dark"; let b = "dark"; ```
Exercise 2Implement
Write `parseCount(input: unknown): number | null` that returns the number when the input is a finite number or a numeric string, and null otherwise. Do not use any.
starter.typescriptexport function parseCount(input: unknown): number | null { // your code }Reveal the reference answer
reference.typescriptexport function parseCount(input: unknown): number | null { if (typeof input === "number" && Number.isFinite(input)) return input; if (typeof input === "string") { const n = Number(input); return Number.isFinite(n) ? n : null; } return null; }Authored expected output: Compiles with no errors. Runtime: parseCount(3) is 3, parseCount("4") is 4, parseCount("x") is null, parseCount(null) is null.
Starting from unknown forces a narrowing branch for each accepted shape.
Exercise 3Debug
The public API below leaks any. Fix the signature so callers get a checked type, keeping the runtime behavior. ```ts function firstItem(items) { return items[0]; } ```
starter.typescriptfunction firstItem(items) { return items[0]; }Reveal the reference answer
reference.typescriptfunction firstItem<T>(items: readonly T[]): T | undefined { return items[0]; }Authored expected output: Compile error resolved: Parameter 'items' implicitly has an 'any' type. Callers now receive T | undefined.
The generic signature removes the implicit any and preserves the element type.
Exercise 4Explain
When is a function's return type `never`, and why does that help exhaustiveness checks?
Reveal the reference answer
reference.typescriptA function returns never when it cannot finish normally, for example it always throws or loops forever. Because never is assignable to every type but no value has type never, a switch that handles every union member narrows the remaining value to never. If a new member is added, the default branch that passes the value to a never-typed function stops compiling, which surfaces the missing case.Authored expected output: A written explanation linking never to unreachable code and exhaustive switches.
never encodes impossibility, so it is the tool for proving a branch is unreachable.
Common misconceptions
#- any and unknown behave the same.
- never is just another word for undefined.
- You should annotate every local variable.
Follow-up practice
#- typescript-unions-narrowing
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.