switch·Esc back

TypeScript

You might say

I've heard there's a stricter version of JavaScript that catches mistakes early. What is it?

A language that checks written code for mismatched names and value types before it runsIf you type user.nmae instead of user.name, TypeScript can flag it before the page runs when the object type is known. It checks declared code contracts; passing those checks does not prove the product logic is right.
Know first
TS
When does `nmae` get caught?
The same task: show a user's name
profile.js
1const user = { name: "Alex" }
2title.textContent = user.nmae
After the page runsundefined
profile.ts
1const user: User = { name: "Alex" }
2title.textContent = user.nmae
Stopped here before runningUser has no nmae. Did you mean name?

TypeScript is like a spell-checker for code: it catches mistakes like this early, but it cannot decide whether your product logic is right.

Why a misspelled property can be flagged early

The same typoThe code asks for nmae when the declared property is name.

JavaScript finds out laterWithout an extra check, the page may reach that line and get undefined at runtime.

TypeScript flags the contractIt compares the code to the types you wrote, usually in the editor, before the code is run.

Learn next
Further reading