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
1
const user = { name: "Alex" }2
title.textContent = user.nmaeAfter the page runsundefined
profile.ts
1
const user: User = { name: "Alex" }2
title.textContent = user.nmaeStopped 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 typo:The code asks for nmae when the declared property is name.
JavaScript finds out later:Without an extra check, the page may reach that line and get undefined at runtime.
TypeScript flags the contract:It compares the code to the types you wrote, usually in the editor, before the code is run.
Learn next
Further reading