What Is TypeScript?
JavaScript with a safety net
A safety net for your JavaScript
TypeScript is not a new language you have to learn from scratch. It is the JavaScript you already know, plus a way to write down what kind of value each variable is allowed to hold. Those little notes are called types, and a tool called the type checker reads them and warns you when something does not add up.
Here is a classic JavaScript mistake. This function is meant to add tax to a price:
function withTax(price, tax) {
return price + tax;
}
withTax('9.99', 0.5);Because price arrived as a string, JavaScript happily glues the two values together and returns the string '9.990.5'. No error, no warning. The bug just sits there until a customer sees a nonsense price on your website.
Now the same function in TypeScript:
function withTax(price: number, tax: number): number {
return price + tax;
}
withTax('9.99', 0.5);The moment you write that last line, TypeScript underlines it in your editor and says something like: Argument of type 'string' is not assignable to parameter of type 'number'. You fix it before anyone ever loads the page.
Why this matters as websites grow
On a tiny script, you can keep every variable in your head. On a real website, with dozens of files, data coming from forms and APIs, and code written by past-you six months ago, that stops working. Types help because:
- Mistakes show up early. You see errors while typing, not after deploying.
- Code explains itself. A function signature like
sendEmail(to: string, subject: string)tells you exactly how to call it. - Editors get smarter. Autocomplete, rename, and jump-to-definition all work better when the editor knows your types.
- Refactoring feels safe. Change a function, and TypeScript points at every place that now needs updating.
Good to know: browsers cannot run TypeScript directly. Before your site goes live, TypeScript is compiled into plain JavaScript, and the type notes are removed. The types exist to help you while you write; the browser only ever sees ordinary JavaScript.
Everything you know still counts
Every valid JavaScript program is already almost-valid TypeScript. Variables, functions, arrays, objects, loops, and conditions all work exactly the same way. In this course we simply add types on top, one small idea at a time.
Quick check
Q1 What happens to TypeScript code before it runs in a browser?
Q2 When does TypeScript warn you about a type mistake?
Want to save your progress? It's free.
Create a free account