Skip to content

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 tsc report for user.fristName on an object that has firstName, 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 .js file get the same checks, with // @ts-check and 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 for user.fristName with its 'Did you mean' suggestion and exit status 1, then Node's undefined and 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 by tsc --allowJs under // @ts-check and a JSDoc @param

See also

Sources to start from