Catch a type error in TypeScript
See how TypeScript stops a bug before the code runs. Press Run to try it, then change the code and run it again.
function double(n: number): number {
return n * 2;
}
// Fix the line below: the age arrives as text.
const age = '21';
console.log(double(age));
Output
Press Run to see the output.
More TypeScript examples
- Hello, World · Print a line of text with a typed variable.
- Interfaces and objects · Describe the shape of an object and use it in a function.
- Generics · Write one function that works with any type, safely.
- Union types and narrowing · Handle values that can be one of several types.
- Async and await · Wait for promises with typed results.