HomepageExploring TypeScript (TS 5.8 Edition)
You can support this book: buy it or donate
(Ad, please don’t block.)

7 Trying out TypeScript without installing it

This chapter gives tips for quickly trying out TypeScript.

7.1 The TypeScript Playground

The TypeScript Playground is an online editor for TypeScript code. Features include:

The Playground is very useful for quick experiments and demos. It can save both TypeScript code snippets and compiler settings into URLs, which is great for sharing such snippets with others. This is an example of such a URL:

https://www.typescriptlang.org/play/?#code/«base64»

Many social media services limit the characters per post, but not the characters per URL. Therefore, we can use Playground URLs to share code that wouldn’t fit into a post.

7.2 A simple TypeScript playground via node --watch

This is the basic approach:

Note that Node.js does not type-check the code. But the type checking we get in TypeScript editors should be enough in this case (since we are only working with a single file).

7.2.1 More configuration: tsconfig.json and package.json

For more sophisticated experiments, we may need two additional files:

I have created the GitHub repository nodejs-type-stripping where both are already set up correctly.

7.2.2 Further reading

7.3 Running copied TypeScript code via Node.js

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:

On macOS, I added the following line to my .zprofile:

alias pbts='pbpaste | node --input-type=module-typescript'