Live data from Hacker News

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

nodejs.org

71–80 of 275 posts

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

#71
post #14

Earlier quoted context omitted.

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…

> Typescript is build time type checked, there is no runtime component to TypeScript.

Not exactly. Typescript enums and decorators are examples.

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

#72

I think this + node:test makes Node.js a pretty compelling sensible default for most things now. Running things with `tsx` was such a QoL improvement when it happened, but it didn't solve everything. Runtime type assertion at the edges is mostly solved through `zod` and tools like `ts-rest` and `trpc` makes it so much easier to do full-stack Typescript these days.

Does this run tsx? Even with the types stripped you still need the JSX transformed to JavaScript

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

#73
Contrary to the popular trend, I usually stick with Node and NPM for most of my projects. That said, I’ve run into plenty of headaches with CommonJS vs ESM quirks. Sharing code between frontend and backend (via shared libraries) still feels messy because of those differences. In one project I actually had to switch to Bun as the runtime, since Node kept failing at runtime, and strangely enough, Bun just worked without issues.

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

#74
post #63
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

Have them though? On the projects I am involved, they could even not exist, only node LTS releases matter, and the most recent projects are still node 20.

As a quick aside, “them” is an object pronoun, not a subject pronoun. The correct word you needed is “they”.

You couldn’t phrase your original question as a statement “Them have though.” That’s often a quick test for valid English grammar. With the correct pronoun, it makes more sense: “They have though.”

As another example, take this sentence: “Have you seen them though?”

“You” is the subject of that sentence, and “them” is the object.

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

#75
post #47

Is there any movement in the node world for the runtime to be able to ascertain types? Obviously nodejs doesn't do this, but does Bun or Deno or are there plans for any of this in the future?

i don’t think you’ll see this at the typescript level, and you won’t see anything like this that compiles to javascript. specifically, compiling typescript to javascript with runtime checks would not actually be very useful. typescript is pretty ambiguous about a lot of the things that would need explicit definition for runtime safety, and anyways we already have tools for that - it’s called zod. and comprehensive ch…

Yeah, but Zod and all of the runtime schema libraries all kind of add verbosity to the type system compared to say something like Typia[0] which AOT compiles the type checks (and ends up being way more elegant).

Caveat is that there are some restrictions with the compiler and some possible footguns (duplicated declarations bloating code).

[0] https://typia.io/

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

#77
post #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 runti…

Agreed about TS, but Python type annotations are not ignored. They are executed as code (all type annotations are valid expressions) and the results are stored on the module/class/function object that contains the annotated variable

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

#78

I think this + node:test makes Node.js a pretty compelling sensible default for most things now. Running things with `tsx` was such a QoL improvement when it happened, but it didn't solve everything. Runtime type assertion at the edges is mostly solved through `zod` and tools like `ts-rest` and `trpc` makes it so much easier to do full-stack Typescript these days.

Let's see if Sveltr converts their codebase back to TypeScript

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

#79
post #63

Earlier quoted context omitted.

Have them though? On the projects I am involved, they could even not exist, only node LTS releases matter, and the most recent projects are still node 20.

As a quick aside, “them” is an object pronoun, not a subject pronoun. The correct word you needed is “they”. You couldn’t phrase your original question as a statement “Them have though.” That’s often a quick test for valid English grammar. With the correct pronoun, it makes more sense: “They have though.” As another example, take this sentence: “Have you seen them though?” “You” is the subject of that sentence, and “…

Them is fine.

It's short for "Have them [Node bozos improved it], though?"

Or, equally likely it, refers to deno and bun ("deno and bun has really made Node focus and improve", "Have them (deno and bun) really made Node focus and improve, though?")

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

#80
post #43

Earlier quoted context omitted.

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.

> (although do you still need to use the .mjs hack?) Syntax detection is enabled by default in v22.7.0, v20.19.0: https://nodejs.org/api/packages.html#syntax-detection Sounds like the obvious correct solution, making .cjs and .mjs obsolete - unless of course someone uses import() statements exclusively, in which case I need to ask: why?

It is surprising for me to see these features finally being added to Node after such a long time. Especially so when I remember reading discussion after discussion about how something like this wasn't possible. I touched on this in a blog post some time ago [1]. Glad Node is catching up.

[1] https://kilo.bytesize.xyz/an-incorrect-specification

Post reply on HN