node --watch
Now that Node.js has built-in support for TypeScript, we can use it as the foundation of a simple playground that lets us interactively explore TypeScript code.
This is the basic approach:
playground.mts
.mts
so that we don’t need a package.json
file to tell Node.js that .ts
means ESM module.node --disable-warning=ExperimentalWarning --watch ./playground.mts
playground.mts
. Whenever we save it, Node.js re-runs it and shows its output.Alas, we often need two additional files:
tsconfig.json
with our preferred settingspackage.json
with "type":"module"
so that we can use the filename extension .ts
.I have created the GitHub repository nodejs-type-stripping
where both are already set up correctly.
--watch
Watches a file and its imports for changes and re-runs the file whenever that happens.--watch-path
Overrides the default of watching the imports and tells Node.js which files to watch instead.--watch-preserve-output
Disables the clearing of the console before the file is re-run.