Live data from Hacker News

Node.js is able to execute TypeScript files without additional configuration

nodejs.org

11–20 of 275 posts

Re: Node.js is able to execute TypeScript files without additional configuration

#11
post #2

It looks like this works by stripping away the type information, so at best it saves you a transpilation pass and doesn't improve safety.

Yeah agreed - saying "node can execute typescript files" is a bit misleading. More accurate would be "node can find-and-replace type information with spaces from .ts files and try and executing them as if they were plain JavaScript" I suspect this would only handle the most rudimentary and basic typescript files. Once you start using the type system more extensively I suspect this will blow-up in your face. It's kind…

TypeScript 5.8 added a configuration option[1], where the compiler emits errors when using language features that result in code generation (e.g. enums). It's expected that people wanting to run TypeScript through "erase-only" transpilers will use this option.

Of course, everyone else is free to use enums, decorators, class parameters, etc., but for quick prototyping or writing simple scripts in TypeScript, Bun has been good enough for me, and I assume Node will be "good enough" as well.

[1] https://www.typescriptlang.org/docs/handbook/release-notes/t...

Re: Node.js is able to execute TypeScript files without additional configuration

#12
post #2

It looks like this works by stripping away the type information, so at best it saves you a transpilation pass and doesn't improve safety.

I'm using tsx for a project to achieve the same effect. As you said, it saves you from having to set up a build/transpilation step, which is very useful for development. Tsx has a --watch feature built in as well, which allows me to run a server from the typescript source files and automatically restart on changes. Maybe with nodemon and this new node improvement this can now done without tsx. To check types at runti…

Node has had a built-in --watch flag for a while too:

https://nodejs.org/docs/latest/api/cli.html#--watch

Re: Node.js is able to execute TypeScript files without additional configuration

#14
post #2

It looks like this works by stripping away the type information, so at best it saves you a transpilation pass and doesn't improve safety.

Yeah agreed - saying "node can execute typescript files" is a bit misleading. More accurate would be "node can find-and-replace type information with spaces from .ts files and try and executing them as if they were plain JavaScript" I suspect this would only handle the most rudimentary and basic typescript files. Once you start using the type system more extensively I suspect this will blow-up in your face. It's kind…

> node can find-and-replace type information with spaces from .ts files and try and executing them as if they were plain JavaScript

That’s what all the other tools like ts-node and tsx do already.

I’m not sure what more are you expecting to do?

Typescript is build time type checked, there is no runtime component to TypeScript. If you want type checking you run tsc.

I think this is a great step in the right direction by node. It will save transpiration on the server and improve stack traves and whatnot.

> Once you start using the type system more extensively I suspect this will blow-up in your face.

I don’t see why. There isn’t any more runtime information in “complex” TypeSceipt types than in simple ones. It’s all build time - see above.

> What a missed opportunity to do it properly

Please explain in more detail what “doing it properly” means to you. Including the typechecker? If so that wouldn’t make sense - they would be competing with TypeScript itself, they shouldn’t, all the “third party plugins” rely on tsc for type checking.

Re: Node.js is able to execute TypeScript files without additional configuration

#15
post #4

Impressed with what Node is doing the last years, deno and bun has really made Node focus and improve. It was stuck for a while

What are the recent improvements in node itself? Last actually note-worthy improvement I heard of was properly supporting import/export (although do you still need to use the .mjs hack?), but I've been out of the loop here for sometime so would be nice to know what they've added since.

using, memory64, undici, async local storage, ESM import improvements, type stripping, local storage / session storage, env file support, built in file watching. Those are just the ones I mainly remember. There is a lot more.

Re: Node.js is able to execute TypeScript files without additional configuration

#16
post #2

It looks like this works by stripping away the type information, so at best it saves you a transpilation pass and doesn't improve safety.

Yeah agreed - saying "node can execute typescript files" is a bit misleading. More accurate would be "node can find-and-replace type information with spaces from .ts files and try and executing them as if they were plain JavaScript" I suspect this would only handle the most rudimentary and basic typescript files. Once you start using the type system more extensively I suspect this will blow-up in your face. It's kind…

I have been using this feature to remove the transpilation step during development for a rather large monorepo that makes extensive use of very complex types and haven’t noticed any errors whatsoever at runtime.

You’re downplaying this quite a bit. Node being able to execute TS files slashes a lot of tooling in half, especially during development when you want to modify files and retry in quick succession.

Besides, I’m not so sure this cannot be expanded in the future to adopt validation features before stripping the type information. For now it solves some major pain points for a lot of people.

Re: Node.js is able to execute TypeScript files without additional configuration

#17
post #2

It looks like this works by stripping away the type information, so at best it saves you a transpilation pass and doesn't improve safety.

TypeScript never promised improving safety, maybe it’s a common misconception. But TypeScript has no runtime mode or information. You were always at the mercy of running and not ignoring the typechecker. Nothing stopped you from running ts-node or tsx on code with egregious type errors. TypeScript is more like a linter in that regard.

Re: Node.js is able to execute TypeScript files without additional configuration

#18
post #4

Impressed with what Node is doing the last years, deno and bun has really made Node focus and improve. It was stuck for a while

What are the recent improvements in node itself? Last actually note-worthy improvement I heard of was properly supporting import/export (although do you still need to use the .mjs hack?), but I've been out of the loop here for sometime so would be nice to know what they've added since.

Small but lovely addition for me is the ability to load .env files natively. There’s more like this; small, focused, real-world-improving features.

Re: Node.js is able to execute TypeScript files without additional configuration

#19
post #12

Earlier quoted context omitted.

I'm using tsx for a project to achieve the same effect. As you said, it saves you from having to set up a build/transpilation step, which is very useful for development. Tsx has a --watch feature built in as well, which allows me to run a server from the typescript source files and automatically restart on changes. Maybe with nodemon and this new node improvement this can now done without tsx. To check types at runti…

Node has had a built-in --watch flag for a while too: https://nodejs.org/docs/latest/api/cli.html#--watch

Surprisingly this gone unheralded.

Re: Node.js is able to execute TypeScript files without additional configuration

#20
post #2

It looks like this works by stripping away the type information, so at best it saves you a transpilation pass and doesn't improve safety.

One of Typescript's design goals is that removing all type-related parts of the source text should yield a valid JavaScript file. A typescript compiler does not generate code (unlike, say, PureScript).

You can run a typechecker (such as tsc) that check various properties of your code statically, relying on the type information. It is then erased.

The same applies, say, to Python: type annotations are ignored at runtime. Somehow similarly, Java's type information is also partly erased in the bytecode; in particular, all the information about parametrized types. (This is to say nothing about actual machine code.)

Post reply on HN