node --watchNow that Node.js has built-in support for TypeScript, we can use it as the foundation of simple playgrounds that let 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 --watch ./playground.mts
playground.mts. Whenever we save it, Node.js re-runs it and shows its output.At the moment, Node’s built-in support for TypeScript still prints the following warning every time we use it:
ExperimentalWarning: Type Stripping is an experimental feature and
might change at any time
We can switch off that warning via the CLI option --disable-warning=ExperimentalWarning – e.g.:
node --disable-warning=ExperimentalWarning --watch ./playground.mts
tsconfig.json and package.json 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.
For simple experiments, it can be enough to simply copy TypeScript code and run it via Node.js:
«paste-command» | node --input-type=module-typescript
«paste-command» depends on your operating system:
pbpasteGet-ClipboardOn macOS, I added the following line to my .zprofile:
alias pbts='pbpaste | node --input-type=module-typescript'
--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.