Native Typescript in Node.js

Did you know that you can use Typescript "natively" with any Node.js-project, similar to Deno? The setup is really simple and quick, as all that's really needed is installing a dependency, ts-node.

As with every project that makes use of NPM, I highly recommend installing ts-node as a local dev-dependency in an existing NPM-project:

npm i -D ts-node

# You also need Typescript as a dependency
npm i -D typescript

If you haven't set up a NPM project, you can quickly do it like this before installing ts-node:

npm init -y
 
# Add a template ts-config.json
npx tsc --init

Now cd into the project, if not already done, and create a new Typescript-file index.ts:

// Here's our little demo. Notice that we're
// using a typed paramter for 'getGreeting'. 

function getGreeting(name: string){
  return "Hello, " + name;
}

console.log(getGreeting("Tom"));

Finally, let's run it with our type-safe REPL:

npx ts-node index.ts

And that's about it! Of course you can use npx ts-node everywhere on your device, thanks to NPX, and simple start a new and empty Node-process. Thanks for the read!

- Tom


expressFlow is now Lean-Forge