Introduction
JavaScript lets you pass anything to a function — a number, a string, or nothing at all — and only complains when the code actually runs. TypeScript adds a layer on top: you describe what shape your data should have, and the editor warns you the moment you get it wrong.
TypeScript does not replace JavaScript. It compiles down to plain JavaScript, so it runs everywhere JavaScript runs. You are adding safety checks, not learning a new language from scratch.
Your first type annotation
A type annotation is just a label after a variable or parameter, saying what it should be.
function greet ( name : string ): string { return `Hello, ${ name } !` ; } greet ( " Sara " ); // Works greet ( 42 ); // Error: Argument of type 'number' is not assignable to 'string'
Enter fullscreen mode
Exit fullscreen mode
That error appears in the editor before you run anything.…