Simple TypeScript playground via node --watch

[2025-02-25] dev, typescript
(Ad, please don’t block)

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.

A simple playground  

This is the basic approach:

  • We create a file playground.mts
    • The filename extension is .mts so that we don’t need a package.json file to tell Node.js that .ts means ESM module.
  • We run the following command in a terminal:
    node --disable-warning=ExperimentalWarning --watch ./playground.mts
    
  • We edit playground.mts. Whenever we save it, Node.js re-runs it and shows its output.

More configuration  

Alas, we often need two additional files:

  • A tsconfig.json with our preferred settings
  • A package.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.

Further reading