Compile-time types, runtime values
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
#- Describe how TypeScript annotations are erased before execution.
- Distinguish a compile-time error from a runtime failure.
- Explain why `as` asserts instead of validates.
Overview
#Why types disappear
TypeScript is JavaScript plus a static type layer that exists only while the program is being checked. When the compiler emits JavaScript, every annotation, interface and generic parameter is erased. The runtime engine never sees them.
function add(a: number, b: number): number {
return a + b;
}
Emitted JavaScript:
function add(a, b) {
return a + b;
}
The number annotations are compile-time only. If a caller hands add a string from plain JavaScript, nothing stops it at runtime.
Two different failure points
- Compile-time: the type checker decides whether the program is well-typed. Errors arrive before anything runs.
- Runtime: the emitted JavaScript executes. Types are gone, so a value can still be wrong.
const value = JSON.parse('"hello"') as number; // compiles
console.log(typeof value); // "string" at runtime
as is a compile-time assertion. It silences the checker; it does not convert or validate the value.
Consequences
- Validate untrusted input at runtime (JSON, network responses, forms, storage). Types cannot do it for you.
interfaceandtypeexist only in the type system and cannot be used as values.- Use
import typefor type-only imports so bundlers can drop them. - A type guard is a runtime function whose return type carries compile-time information.
Mental model
Treat TypeScript as a very good linter plus editor intelligence. It proves things about the code you wrote, not about the data that arrives while it runs.
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
This code compiles. What does it print at runtime? ```ts const raw: unknown = "42"; const n = raw as number; console.log(typeof n); ```
Exercise 2Explain
Explain why `as` does not protect a program at runtime, using `JSON.parse` as the example.
Reveal the reference answer
reference.typescript`JSON.parse` returns `any`, so the checker accepts `as number` without evidence. `as` is a claim about the type, not a check of the value. At runtime the parsed value is whatever the string contained, so a caller can still receive a string where the type says number. Real safety needs a runtime guard that inspects the value.Authored expected output: A written explanation that names type erasure and the absence of a runtime check.
The key idea is that assertions move responsibility from the compiler to the programmer.
Exercise 3Implement
Write a runtime validator `isNonEmptyString(value: unknown): value is string` that returns true only for strings whose length is greater than zero.
starter.typescriptexport function isNonEmptyString(value: unknown): value is string { // your code }Reveal the reference answer
reference.typescriptexport function isNonEmptyString(value: unknown): value is string { return typeof value === "string" && value.length > 0; }Authored expected output: Compiles with no errors. Runtime: isNonEmptyString("a") is true, isNonEmptyString("") is false, isNonEmptyString(1) is false.
The `value is string` return type is compile-time information; the `typeof` test is the real runtime check that makes the claim true.
Exercise 4Debug
This file fails to compile. Fix it without changing what the program does. ```ts interface Point { x: number; y: number; } console.log(Point); ```
starter.typescriptinterface Point { x: number; y: number; } console.log(Point);Reveal the reference answer
reference.typescriptInterfaces are erased and cannot be used as values. Log a real object instead: ```ts interface Point { x: number; y: number; } const origin: Point = { x: 0, y: 0 }; console.log(origin); ```Authored expected output: Compile error resolved: 'Point' only refers to a type, but is being used as a value here.
The fix separates the type-level declaration from the runtime value that is logged.
Common misconceptions
#- A value typed as number is guaranteed to be a number at runtime.
- `as` performs a runtime conversion or validation.
- Interfaces exist as objects in the emitted JavaScript.
Follow-up practice
#- typescript-primitives-inference
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.