TypeScript is JavaScript with added type annotations that a compiler checks before the code runs, then removes, so what actually runs is ordinary JavaScript. JavaScript finds type mistakes only when the faulty line executes; TypeScript reports many of them in your editor or build, which is the whole difference in practice.
The TypeScript handbook puts it in one line: "TypeScript is JavaScript's runtime with a compile-time type checker." The examples below were run with TypeScript 5.9.3 and Node.js 18.
TypeScript vs JavaScript at a glance
| JavaScript | TypeScript | |
|---|---|---|
| File extensions | .js, .mjs, .cjs | .ts, .mts, .cts, .tsx |
| Types | Dynamic, checked at run time | Static annotations, checked before running |
| Runs in browsers / Node | Directly | After compiling or stripping types |
| Build step | Optional | Needed (compiler, bundler or type stripping) |
| Runtime speed | Baseline | Identical: types are erased |
| Editor help | Good (often powered by TypeScript in the background) | Better: precise autocomplete, rename, go to definition |
| Config | None needed | tsconfig.json |
The same bug in JavaScript and TypeScript
This JavaScript runs without complaint and gives a wrong answer:
function total(price, qty) {
return price * qty;
}
console.log(total(19.99, "3"));
console.log(1 + "3");
Output:
59.97
13
The multiplication happens to work because * converts "3" to a number, but + concatenates strings, so 1 + "3" is the string "13". Nothing warns you; the bug surfaces wherever the wrong value ends up. MDN describes JavaScript as a language with dynamic types that is also weakly typed: it "allows implicit type conversion when an operation involves mismatched types, instead of throwing type errors".
The TypeScript version adds types to the parameters and an optional property to an object type:
function total(price: number, qty: number): number {
return price * qty;
}
const q = "3";
console.log(total(19.99, q));
interface User { name: string; email?: string }
const u: User = { name: "alex" };
console.log(u.email.toLowerCase());
Running npx tsc --strict --noEmit price.ts prints:
price.ts(5,26): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
price.ts(9,13): error TS18048: 'u.email' is possibly 'undefined'.
The second error is the one that pays for TypeScript in most codebases. In JavaScript, that line would crash at run time with TypeError: Cannot read properties of undefined (reading 'toLowerCase'), the error covered in Cannot read properties of undefined. TypeScript flags it before the code ever runs, because the type says email may be missing.
What the compiled output looks like
Types disappear on compilation. This TypeScript:
interface User { name: string; email?: string }
function greet(u: User): string {
return `Hi ${u.name}`;
}
console.log(greet({ name: "alex" }));
compiled with npx tsc --strict --target es2022 ok.ts becomes:
"use strict";
function greet(u) {
return `Hi ${u.name}`;
}
console.log(greet({ name: "alex" }));
The interface and every annotation are gone. The handbook describes this as erasing the types, and states the rule behind it: "TypeScript never changes the runtime behavior of JavaScript code." Two consequences follow. TypeScript cannot be faster or slower than the equivalent JavaScript. And types offer no protection at run time: if an API returns data that doesn't match your interface, nothing stops it, so validate external input as you would in JavaScript.
By default tsc still writes the JavaScript file when there are type errors: compiling the buggy price.ts above printed both errors, exited with code 2, and produced price.js anyway (the noEmitOnError option changes that). A type error is a warning that the code is probably wrong, not a syntax error that stops it running.
What's the same in both
Everything except types. The handbook says "JS syntax is therefore legal TS", and the runtime is JavaScript's. Scoping rules (var, let and const), equality (== vs ===), promises and async/await, the DOM and Node APIs, and npm packages all work identically. Learning TypeScript means learning the type syntax: annotations, interfaces and type aliases, unions such as string | null, generics, and how the compiler narrows types after checks like if (u.email).
Running TypeScript
- The compiler:
npm install --save-dev typescript, thennpx tsctype-checks and emits JavaScript according totsconfig.json.npx tsc --initcreates one; in TypeScript 5.9 the generated file enables"strict": truealong with stricter options such asnoUncheckedIndexedAccess. - Bundlers and frameworks compile
.tsas part of their build, but may not check types. Vite's docs say it "only performs transpilation on .ts files and does NOT perform type checking", leaving that to your editor and build process, so runtsc --noEmitin CI. - Node.js directly: Node added type stripping in v22.6.0 and enabled it by default in v22.18.0 and v23.6.0; it was marked stable in v24.12.0 and v25.2.0.
node app.tsthen runs the file, but the Node docs are clear that it performs no type checking, ignorestsconfig.json, requires full file extensions in imports, and rejects syntax that needs code generation, such asenumand namespaces with runtime code.
Path aliases from tsconfig.json are a common stumbling block when moving code between these options; Error: Cannot find module explains why.
You can type-check JavaScript without converting it
TypeScript's checker also reads JSDoc comments in plain .js files. Add // @ts-check at the top:
// @ts-check
/** @param {number} price @param {number} qty */
function total(price, qty) {
return price * qty;
}
console.log(total(19.99, "3"));
npx tsc --noEmit --allowJs --checkJs checked.js reports the same TS2345 error as the .ts version. This is a low-risk way to get most of the checking in an existing JavaScript project with no build step, and to migrate file by file later.
Which should you use?
- Use TypeScript for applications and libraries that several people maintain or that live longer than a few months. The checker catches typos, wrong argument types and missing null checks, and makes renaming across a large codebase safe.
- Use JavaScript for small scripts, quick prototypes, snippets in HTML pages, and anywhere a build step costs more than it saves. Add
// @ts-checkif you want some checking anyway. - Learning: learn JavaScript first; TypeScript's types make sense only once you know the runtime behaviour they describe.
Switching is incremental. Set "allowJs": true in tsconfig.json, rename files from .js to .ts one at a time, and fix the errors each rename reveals. Turning on strict from the start of a new project is much easier than enabling it later on a large codebase.
Frequently asked questions
- Is TypeScript faster than JavaScript?
- No. TypeScript compiles to JavaScript and the types are erased, so the code that runs is plain JavaScript and performs the same. The TypeScript handbook states that TypeScript never changes the runtime behavior of JavaScript code. The benefit is fewer bugs reaching runtime, not speed.
- Can browsers run TypeScript directly?
- No. Browsers run JavaScript, so TypeScript must be compiled or have its types stripped first, usually by a bundler or framework build step. Recent Node.js versions can run .ts files directly by stripping the types, but they do not type-check them.
- Is all JavaScript valid TypeScript?
- Syntactically, yes: the handbook says JS syntax is legal TS. But the type checker may report errors on valid JavaScript, such as passing a string where it infers a number. You can still compile and run that code; the errors are warnings about likely bugs that you choose to fix or suppress.
- Should a beginner learn JavaScript or TypeScript first?
- JavaScript first. Everything TypeScript adds sits on top of JavaScript's behaviour: scoping, equality, async code and the standard library are identical. Once those are familiar, TypeScript's types are a small extra layer that is quick to pick up.