Back to Home

Node.js Now Has Native Typescript Support

RSS Feed
Veldrine Evelia Kaharwa2 min read
Node.js Now Has Native Typescript Support

How to Run TypeScript code using Node.js

Since V22.6.0, Node.js has experimental support for some TypeScript syntax via "type stripping". This means that you can write code that's valid TypeScript directly in Node.js without the need to transpile it first. Let’s see an example:

If we have a simple TypeScript program hello.ts below

typescript
1function capitalize(word: string): string {   
2
3 return word.charAt(0).toUpperCase() + word.slice(1); 
4
5}
6 function hello(name: string): string {   
7
8 return "Hello " + capitalize(name);
9
10  }
11  console.log(hello("Peter"))
12
13

We can run the program below using Node.js by running this command on your terminal

bash
1node --experimental-strip-types hello.ts

Since Node v23+, --experimental-strip-types is enabled by default and therefore we can run a TypeScript program using the command node hello.ts

What really happens to the code when run by Node.js?

By default Node.js will execute TypeScript files that contain only erasable TypeScript syntax. This means that Node.js will replace TypeScript syntax with whitespace, and no type checking is performed. This means that our hello program above would be transformed to something like

javascript
1function capitalize(word ) {   
2
3 return word.charAt(0).toUpperCase() + word.slice(1);
4
5  }
6  function hello(name ) {   
7
8return "Hello " + capitalize(name); 
9
10 }
11  console.log(hello("Peter"))
12
13

Node.js ignores tsconfig.json files and therefore features that depend on settings within tsconfig.json, such as paths or converting newer JavaScript syntax to older standards, are intentionally unsupported.

What if you need Full TypeScript support

To use TypeScript with full support for all TypeScript features, including tsconfig.json, you can use a third-party package. For example using the tsx library you can run the following command to install it as a dev dependency in your project.

bash
1npm install --save-dev tsx

You can then run your code with the command

bash
1npx tsx your-file.ts

or

bash
1node --import=tsx your-file.ts.

Conclusion

TypeScript support in Node.js is still an experimental feature at this time and there may be changes along the way. Because of these limitations,there is no type checking or linting built into the runtime currently. If you prefer a fully fledged JavaScript runtime with builtin TypeScript support please check out Deno. You can learn more about Deno at https://deno.com/.

Reactions

About the Author

Veldrine Evelia Kaharwa's profile picture

Veldrine Evelia Kaharwa

I'm a software developer passionate about building scalable and efficient applications. I enjoy writing code using Rust,TypeScript and Java. I'm currently a student at Kirinyaga University pursuing a Bachelor Of Science Degree in Computer Systems Engineering.

Comments

No comments yet. Be the first to comment!

Add a Comment