What is TypeScript?
JavaScript with an extra layer that checks the *shape* of your data before your code ever runs.
What is it?
Here is a plain JavaScript function that adds two numbers:
function add(a, b) { return a + b; }
Nothing stops you from calling add("5", 10) by mistake. JavaScript quietly turns "5" + 10 into the string "510". You only find the bug later, maybe in production.
Now imagine a tool that reads your code before it runs and says: "add expects two numbers, but you're passing a string." You describe what kind of value each variable, parameter and return value should be, and the tool checks that your code matches. This is called static typing: "static" because the check happens by reading the code, not running it.
That tool is TypeScript. It's not a separate language. It's JavaScript plus optional type annotations. Browsers and Node.js don't understand those annotations, so a compiler strips them out of your .ts file and produces plain JavaScript (.js), the same code you'd write by hand, just checked first.
Explain like I'm 10
It's like a spell-checker for your code's data. You can write what you want, but it underlines what doesn't make sense, like a phone number where an email was expected, so you can fix it before anyone sees it.
Examples
A mistake JavaScript won't catch
// plain JavaScript
function add(a, b) {
return a + b;
}
add("5", 10); // returns "510", no error, no warningJavaScript runs this without complaint. The bug is silent: you just get a weird result.
The same mistake in TypeScript
// TypeScript
function add(a: number, b: number): number {
return a + b;
}
add("5", 10);
// Error: Argument of type 'string' is not assignable
// to parameter of type 'number'.The : number after each parameter is a type annotation. TypeScript flags the call as invalid before anything runs, right in your editor, often as you type.
How it works
TypeScript code lives in .ts files. The TypeScript compiler (tsc) checks every type annotation against how the values are used and reports mismatches as errors.
Then it strips the annotations out and writes plain .js files that any browser or Node.js can run. Types never exist at runtime. That's why TypeScript is called a "compile-time" layer: it does its job before the program runs, then gets out of the way.
you write: app.ts (JavaScript + type annotations)
↓
TypeScript compiler checks types, reports errors
↓
strips the types out
↓
you get: app.js (plain JavaScript)
↓
runs in browser / Node.jsWhy does it exist?
As JavaScript projects grow to hundreds of thousands of lines, small mistakes become more common and harder to track down: passing the wrong kind of value, misspelling a property, forgetting a value might be missing. JavaScript can't describe what a function expects, so these mistakes only show up when the code runs, sometimes in front of users.
Microsoft created TypeScript (first released in 2012) so large codebases could state those expectations. Tools can then catch violations right away instead of relying only on tests and code review.
When to use it
Use TypeScript on any project that will grow, have more than one developer, or live long enough that "what does this function expect?" gets hard to remember. It pays off most in teams, in libraries other code depends on, and wherever refactoring needs to feel safe.
When not to use it
For a throwaway script, a quick experiment or a tiny snippet, plain JavaScript is often faster. TypeScript also adds a build step. If that overhead doesn't pay for itself, skip it.
Common mistakes
Thinking TypeScript is a different language. It's JavaScript plus type annotations that are removed before the code runs.
Believing TypeScript makes code faster. It has no effect on runtime performance; it only catches mistakes earlier.
Assuming type errors stop the program from running. By default the compiler still outputs JavaScript, unless you configure it not to.
Practice exercises
- Easy:
Write a JavaScript function
multiply(a, b)with no types. Then rewrite it in TypeScript with: numberannotations on both parameters and the return value. - Medium:
Call your TypeScript function with a string argument and read the error. Write down in your own words what it's telling you.
- Hard:
Without using the word "TypeScript", explain what problem static typing solves that plain JavaScript can't, as if to someone who has never programmed.
Interview questions
What is TypeScript, in relation to JavaScript?
A superset of JavaScript that adds optional static type annotations. A compiler checks them before the code runs, then strips them and outputs plain JavaScript.
Does TypeScript code run directly in the browser?
No. It's compiled to plain JavaScript first. Browsers and Node.js only run the resulting JavaScript.
What is static typing, and what problem does it solve?
Checking that values match their expected types by reading the code, before it runs. It catches mistakes like passing the wrong kind of value to a function at compile time instead of at runtime.
Why is TypeScript called a *superset* of JavaScript rather than a separate language?
Every valid JavaScript program is already valid TypeScript. TypeScript only adds optional type syntax on top; it doesn't remove or replace any JavaScript feature.
Does using TypeScript make your code run faster at runtime?
No. By the time the code runs, it's plain JavaScript with all types stripped out. TypeScript only helps you catch mistakes earlier.
If your TypeScript file has type errors, does `tsc` still produce a `.js` file by default?
Yes. By default it reports the errors but still emits JavaScript. You must opt in (for example with noEmitOnError) to make errors block the build.
What are the TypeScript compiler's two main jobs?
First, it checks every type annotation against how values are used and reports mismatches. Second, whether or not it found errors, it strips the annotations and writes plain JavaScript that any browser or Node.js can run.
Why is TypeScript's checking described as happening "at compile time" instead of "at runtime"?
The compiler finds mismatches just by reading your code; it never runs it. That's what "compile time" means, as opposed to a bug that only appears while the program runs.
Since type annotations are erased before the code runs, can you check a variable's declared TypeScript type at runtime with `typeof`?
No. typeof only tells you the actual JavaScript value's type ("string", "number", and so on). The TypeScript annotation no longer exists at runtime, so there's nothing to inspect.
What is *type erasure*, in the context of TypeScript?
The compiler removing every type annotation, interface and type alias before producing JavaScript. It's why you can never inspect or rely on TypeScript types at runtime.
A function like `function add(a, b) { return a + b; }` in plain JavaScript accepts `add("5", 10)` without complaint. What does it actually do, and why doesn't TypeScript's equivalent have the same problem?
JavaScript silently coerces the string and returns "510". In the TypeScript version, function add(a: number, b: number): number, the compiler checks the call against the annotations and flags the string argument before the code runs.
Is it accurate to say TypeScript is a completely different language from JavaScript that has to be learned from scratch?
No, this is a common misconception. TypeScript is JavaScript with an optional layer of type annotations. If you know JavaScript, you already know most of TypeScript; the new part is the type syntax.
Why might static typing catch bugs "while you're still typing," rather than only when you run `tsc` from the command line?
Editors run the TypeScript checker in the background and underline mismatches as you write. You don't need to run a separate command to see the error.
Why does static typing tend to matter more as a JavaScript project grows from a few hundred lines to hundreds of thousands?
In a small script you can remember what every function expects. As the codebase and team grow, that stops being reliable, and mistakes like passing the wrong shape of value become more likely and harder to trace. Compiler-checked types prevent exactly that.
For a five-minute throwaway script, why might plain JavaScript be a better choice than TypeScript?
TypeScript adds a build step and needs type annotations. For code you'll run once and throw away, that overhead can cost more than it saves, since there's no future maintenance or team to protect.
A teammate suggests annotating every single variable in a codebase explicitly, even ones like `let count = 0`. Is that necessary?
No. TypeScript infers the type from the initializer, so let count = 0 is already number. Annotations matter most where the type isn't obvious, such as function parameters.