Introduction
TypeScript is a strongly typed superset of JavaScript that adds optional static types, catching many errors before the code runs and compiling down to plain JavaScript that runs anywhere JavaScript does.
Learn Python, JavaScript, Java and more with free interactive lessons, real projects and AI-powered help. Beginner-friendly.
Part of the free TypeScript course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll understand what TypeScript is, why static types catch bugs before your code runs, how the tsc compiler turns TypeScript into JavaScript, and you'll have written your first typed variable.
Think of TypeScript as the spell-checker and grammar-checker in a word processor. JavaScript lets you type anything and only notices a problem when someone reads the document out loud (at runtime). TypeScript underlines the mistake as you type — before anyone runs the program. The document you finally print is identical plain text either way; the checker just stops embarrassing errors from shipping. TypeScript checks your types the same way, then steps out of the way, leaving ordinary JavaScript to run.
1. What Is TypeScript?
TypeScript is an open-source language built and maintained by Microsoft. It is a superset of JavaScript — meaning every valid JavaScript program is already a valid TypeScript program. On top of plain JavaScript, TypeScript adds one big feature: static type checking . A "type" is just the kind of value something holds — text, a number, true/false — and "static" means TypeScript checks those kinds before the program runs, not while it's running.
Because it's a superset, you can adopt TypeScript gradually. You don't rewrite anything; you rename a .js file to .ts and start adding type information wherever it helps.
TypeScript = JavaScript + Static Types. Any JavaScript you already know still works. TypeScript layers an optional, checkable description of your data on top — and then disappears, leaving plain JavaScript to actually run.
2. Why Static Types Help
JavaScript is dynamically typed : a variable can hold text now and a number a moment later, and nobody complains until the program actually trips over it at runtime — often in front of a user. That freedom is handy for tiny scripts but turns into hard-to-find bugs in real apps.
Static types flip this around. You describe what each variable should hold, and TypeScript verifies it at compile time — before a single line runs. The payoff:
In JavaScript , this runs and silently produces nonsense — no error, just a bug:
In TypeScript , the same mistake is stopped before the program runs:
3. TypeScript vs JavaScript
They share the same syntax and run the same way in the end — TypeScript just adds a checking step first. Here's the side-by-side.
4. How tsc Compiles TS → JS
Browsers and Node.js don't understand TypeScript — they only run JavaScript. So before you run anything, the TypeScript compiler (the tsc command) does two jobs: it checks your types, then erases them, leaving plain JavaScript. This erase step is why types cost you nothing at runtime — they vanish from the final code.
The example below is the actual JavaScript output of a typed program — the comments show the TypeScript you'd write, the code is what runs. Press Run to see the result.
5. Basic Setup
TypeScript runs on Node.js. If you don't have it, install it from nodejs.org . Then add TypeScript to your project (the recommended way) and create a config file:
6. Your First Typed Variable
A type annotation is the heart of TypeScript. You write the variable name, a colon, then the type: let age: number = 25; . The three types you'll use constantly are number , string , and boolean .
When the type is obvious from the value, you can skip the annotation and let type inference figure it out — you still get full safety. Now try it. The boxes below run as JavaScript (the comments show the matching TypeScript). Fill in each ___ and press Run.
One more. TypeScript's whole job is to keep a variable's type consistent — a number stays a number. Fill in two numbers so the calculation works:
Here's a small, realistic program. Every variable would carry a type in a .ts file (shown in the comments), so a stray string in a number slot would be caught before it ever ran. Run it and tweak the numbers.
Q: Do I have to rewrite my JavaScript to use TypeScript?
No. Every .js file is already valid TypeScript. Rename it to .ts and add types only where they help. You can convert a project file by file.
Q: Does TypeScript make my program run faster?
No — the types are erased during compilation, so the JavaScript that runs is the same speed. The benefit is fewer bugs and better tooling while you write, not faster execution.
Q: Why can't I just run a .ts file with node ?
Node and browsers only understand JavaScript. You compile first with tsc to get a .js file, then run that. Tools like ts-node bundle both steps for convenience.
Q: Do I have to write a type on every single variable?
No. TypeScript infers the type from the value, so let n = 5; is already typed as a number. Annotate when the type isn't obvious — function parameters are the most common case.
No blanks this time — just a brief and a starter outline. Declare the variables yourself, print the lines, and check your output against the example in the comments. (Write it as runnable JavaScript; the comments note the TypeScript type each variable would have.)
Practice quiz
What is TypeScript best described as?
- A replacement for JavaScript that runs in browsers directly
- A JavaScript testing framework
- A strongly typed superset of JavaScript that compiles to plain JavaScript
- A new browser engine
Answer: A strongly typed superset of JavaScript that compiles to plain JavaScript. TypeScript is a superset of JavaScript that adds static types and compiles down to plain JavaScript.
When does TypeScript check your types?
- At compile time, before the program runs
- At runtime, while the program executes
- Only when an error is thrown
- Never; types are just documentation
Answer: At compile time, before the program runs. Static types are checked at compile time, catching many bugs before the code ever runs.
What happens to type annotations when TypeScript compiles to JavaScript?
- They are kept and enforced at runtime
- They are converted to runtime assertions
- They are turned into comments
- They are erased, leaving plain JavaScript
Answer: They are erased, leaving plain JavaScript. tsc checks the types, then erases them, so types cost nothing at runtime.
Which command compiles TypeScript files to JavaScript?
- node
- tsc
- npm run
- ts-build
Answer: tsc. The TypeScript compiler is invoked with the tsc command (or npx tsc inside a project).
How do you write a typed variable annotation in TypeScript?
- let age: number = 25
- let age = number 25
- let number age = 25
- let age <number> = 25
Answer: let age: number = 25. The syntax is name, colon, type, then value: let age: number = 25;
Since every JavaScript program is also valid TypeScript, TypeScript is called a what?
- A subset of JavaScript
- A fork of JavaScript
- A superset of JavaScript
- A dialect unrelated to JavaScript
Answer: A superset of JavaScript. Every valid JavaScript program is already valid TypeScript, so TypeScript is a superset.
What does type inference let you do?
- Run TypeScript without compiling
- Skip annotations when the type is obvious from the value
- Disable all type checking
- Convert types to any automatically
Answer: Skip annotations when the type is obvious from the value. When the type is clear from the value (let n = 5), TypeScript infers it and you keep full safety.
Why can't you run a .ts file directly with node?
- Node deletes .ts files
- TypeScript files are encrypted
- Node only runs .mjs files
- Node only understands JavaScript, so you must compile first
Answer: Node only understands JavaScript, so you must compile first. Node and browsers run JavaScript, so you compile with tsc (or use ts-node) first.
Which tsconfig setting turns on the most bug-catching checks?
- "loose": true
- "strict": true
- "checkBugs": true
- "safe": true
Answer: "strict": true. Setting "strict": true enables the checks where most of TypeScript's bug-catching power lives.
In JavaScript, total = 100 then total = "free" is allowed. What does TypeScript do with let total: number = 100; total = "free"?
- Allows it silently
- Converts the string to a number
- Reports a compile error (string not assignable to number)
- Crashes at runtime
Answer: Reports a compile error (string not assignable to number). TypeScript reports TS2322: type 'string' is not assignable to type 'number' before the code runs.