What TypeScript adds — types that are checked, then erased¶
Level: 101 · for JavaScript programmers meeting TypeScript
Stub — an outline, not a lesson. There is no runnable example behind this page yet, so nothing on it has been through the check that backs every other claim in this library. The questions below are what the finished page has to answer.
One line: TypeScript is JavaScript plus a checker that runs before the program does: tsc rejects user.fristName and suggests firstName, while Node, which only deletes type syntax and runs the rest, prints undefined.
Keywords: static type checking, type error
What the finished page will answer¶
- What does
tscreport foruser.fristNameon an object that hasfirstName, and what does Node print for the same file? - Which mistakes does the checker catch before a run that JavaScript reports late or never: a misspelt property, a string passed as a number, a method called on a value that may be
undefined? - Does the checker need annotations to find those mistakes, or does it work from the types it infers?
- How does a plain
.jsfile get the same checks, with// @ts-checkand a JSDoc@param {number}? - Which TypeScript features add run-time code instead of only types, and why does this library keep away from them?
Examples it will need¶
-
typescript_adds_a_check_sh.sh— tsc's TS2551 foruser.fristNamewith its 'Did you mean' suggestion and exit status 1, then Node'sundefinedand exit status 0 for the same file -
typescript_adds_checks_to_js_sh.sh— the same misspelling and a wrongly typed argument caught in a plain .js file bytsc --allowJsunder// @ts-checkand a JSDoc@param
See also¶
- Types are erased — what is left of the program once the types are deleted
- Running TypeScript — the two commands, one that checks and one that runs
- The language and the host — what JavaScript itself defines before TypeScript adds anything
anyis contagious — where the checker stops checking without saying so- Rust: What a compiler does before your program runs ↗ — rustc also rejects code before it runs, and its types reach the machine code