What TypeScript is
JavaScript with type annotations, checked before the code runs and then removed. Nothing survives into the output but the JavaScript you already knew how to write.
// what you write
function total(prices: number[]): number {
return prices.reduce((a, b) => a + b, 0);
}
// what actually ships
function total(prices) {
return prices.reduce((a, b) => a + b, 0);
}- Types are erased at build time. They cost nothing at runtime and provide nothing at runtime.
- That second half matters: a value arriving from an API is not checked by TypeScript. It only checked the code you wrote about it.
- Every valid JavaScript file is a valid TypeScript file, which is what makes gradual adoption possible.