Running TypeScript — Node strips the types, tsc checks them¶
Level: 101 · for anyone about to run their first .ts file
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: node server.ts blanks out the types and runs, so a file that assigns "8080" to a number prints 80801 and exits 0; the check is a separate command, tsc --noEmit, which reports TS2322, writes nothing and exits 1.
Keywords: --no-strip-types, process.features.typescript, noEmit
What the finished page will answer¶
- What does
node server.tsprint whenport: numberholds"8080", and what dotsc --noEmitand its exit status say about the same file? - Which Node versions strip types without a flag, what does
process.features.typescriptreport, and what error does a typed file give under--no-strip-types? - What exit status does
tscgive when it finds errors under--noEmit, and when it writes JavaScript despite them? - How does Node decide whether a
.tsfile is an ES module or CommonJS, and what do.mtsand.ctschange? - Why must a relative import say
./util.ts, and whichtsconfig.jsonoption letstscaccept that? - What does Node do with a
.tsfile insidenode_modules, and what doesnode --check app.tsreport?
Examples it will need¶
-
running_typescript_both_commands_sh.sh— Node's80801and exit status 0 for a file with a type error, then tsc's TS2322 and exit status 1 for the same file -
running_typescript_features_sh.sh—process.features.typescriptwith and without--no-strip-types, and the SyntaxError Node gives a typed file when stripping is off
See also¶
- Running a file with Node — the same command for a plain .js file
- Erasable syntax — the TypeScript that Node refuses to run at all
tscandtsconfig.json— configuring the check that Node does not do- Module resolution — why an import names
./util.ts, extension included process— reading the exit statuses both commands return- Rust: Compiled, interpreted, or something between ↗ — when a toolchain compiles, and what runs afterwards