Live data from Hacker News

Node.js adds experimental support for TypeScript

github.com

481–490 of 570 posts

Re: Node.js adds experimental support for TypeScript

#482

Earlier quoted context omitted.

They did just add a new keyword, satisfies, in 5.4. That would be a breaking change if you can’t upgrade the type stripper separately.

This is true, but in other cases they added keywords in ways that could work with type stripping. For example, the `as` keyword for casts has existed for a long time, and type stripping could strip everything after the `as` keyword with a minimal grammar. When TypeScript added const declarations, they added it as `as const` so a type stripping could have still worked depending on how loosely it is implemented. I thin…

It can’t strip what’s after the as keyword without an up-to-date TS grammar, because `as` is an expression. The parser needs to know how to parse type expressions in order to know when the RHS of the `as` expression ends.

Let’s say that typescript adds a new type operator “wobble T”. What does this desugar to?

    x as wobble
    T
Without knowing about the new wobble syntax this would be parsed as `x as wobble; T` and desugar to `x; T`

With the new wobble syntax it would be parsed as `x as (wobble T);` according to JS semicolon insertion rules because the expression wobble is incomplete, and desugar to `x`

Re: Node.js adds experimental support for TypeScript

#483
post #338

Earlier quoted context omitted.

What bad coding practices does TS allow, and why are they bad?

TS allows you to pass a read-only object to a method taking a read-write value: type A = { value: number; } function test(a: A) { a.value = 3; } function main() { const a: Readonly = { value: 1 }; // a.value = 2;

Nice find. Never ran into this because I haven't mutated inputs in over 15 years.

Re: Node.js adds experimental support for TypeScript

#484
post #34

If Node.js can run TypeScript files directly, then the TypeScript compiler won't need to strip types and convert to JavaScript - it could be used solely as a type checker. This would be similar to the situation in Python, where type checkers check types and leave them intact, and the Python interpreter just ignores them. It's interesting, though, that this approach in Python has led to several (4?) different popular…

You can have this now adding types with JSDoc and validating them with typescript without compiling, you get faster builds and code that works everywhere without magic or need to strip anything else than comments. The biggest pain point of using JSDoc at least for me was the import syntax, this has changed since Typescript 5.5, and it's now not an issue anymore.

Good for annotating js files here and there, but lousy dx.

Re: Node.js adds experimental support for TypeScript

#485

If this feature ever becomes the default (ie not behind a flag) - how will the NPM ecosystem respond? Will contributors still bother to build CJS end EJS versions when publishing a NPM module, or just slap an 'engine: nodejs >= 25' on the package.json and stop bothering with the build step before pushing to NPM ? I personally would very much prefer if NPM modules that have their original code in TS and are currently…

I would love to ship my source code (.ts) to npm. But Typescript team was very much against this, as there'll be tsconfig issues and other performance issues. But still fingers crossed.

Re: Node.js adds experimental support for TypeScript

#486

It's about time for TC39 and Microsoft to standardize TypeScript as part of JavaScript. Not "types as comments" either, but actually TypeScript, minus the non-standard runtime semantics and modulo whatever changes are necessary to integrate the grammar. So many runtimes and tools are integrating TypeScript now, and with multiple implementations, that a real standard is necessary. It'll be much harder to evolve TypeSc…

TC39 standardizing TypeScript as part of JavaScript would be a critical error. TC39's job is to look at all the options and make something better. TypeScript won out in popularity due to its tight integration with VSCode. it's not necessarily the best route for the core language.

Re: Node.js adds experimental support for TypeScript

#487

Earlier quoted context omitted.

> Why is making downstream have to switch to `await import()` that big of a deal? > You can use async/await in CJS just fine. Sure, sometimes you may need to resort to some ugly async IIFE wrappers because CJS doesn't support top-level await like ESM does, but is that really such a big deal? It might seem like a small amount of work, but for a library one must to multiply that small amount of work by the number of us…

> multiply that small amount of work by the number of users who will have to repeat it This is probably where we have the biggest difference in our calculations. I know there's a lot of pain in legacy CJS systems, but from my view (which is maybe more "browser-oriented", which is maybe a bit more Deno/Bun-influences, which comes from a "Typescript-first" mentality going way back to 0.x) it is more legacy "giant balls…

Aight let me chime in here.

I'm a game developer. I make web games. We run our games with a simple nginx server that simply serves the wasm. We have some JavaScript libraries we use. They have to be raw dog .js.

I don't even know what your "ejs" or "cjs" acronyms mean.

We use the discord JavaScript SDK. Discord only ships it as a node module or as .ts.

It's a pain in our ass to update because we don't know what those tools your talking about are and we don't want to know. Just give me the damn .js

Re: Node.js adds experimental support for TypeScript

#488
I wonder how this handles import resolution.

type: module requires file extensions and does not support importing folders like most people are used to so it will not be compatible with most existing Typescript code.

Will it transform esm import syntax into require statements?

I'd prefer it break existing code to enforce correctness

Re: Node.js adds experimental support for TypeScript

#489

Earlier quoted context omitted.

I’ll flip this around… reusing comparison as angle brackets is the mistake. C++ ran into some issues too. I think Rust made the really smart move of putting :: before any type parameters for functions. Go made the good move of using square brackets for type parameters.

The problem can be traced back to ASCII/typewriters only including three sets of paired characters, plus inequality signs, which is not enough for programming languages. We really need five sets: grouping, arrays/indexing, records, type parameters, and compound statements. Curly braces {} are also overloaded in JS for records and compound statements, leading to x => {} and x => ({}) meaning different things. Square b…

There is also the possibility to use digraphs such as or other combinations.

(And they could even converted to unicode by the code formatter)

Re: Node.js adds experimental support for TypeScript

#490
post #317

Earlier quoted context omitted.

Flow tries to be sound and that makes it infinitely better than TS where the creators openly threw the idea of soundness out the window from the very beginning.

Can you explain what you mean when you say "to be sound"?

When a language's type system is sound, that means that if you have an expression with type "string", then when you run the program the expression's value will only ever be a string and never some other sort of value.

Or stated more abstractly: if an expression has type T, and at runtime the expression evaluates to a value v, then v has type T.

The language can still have runtime errors, like if you try to access an array out of bounds. The key is that such operations have to give an error — like by throwing, so that the expression doesn't evaluate to any value at all — rather than returning a value that doesn't fit the type.

Both TypeScript and Flow are unsound, because an expression with type "string" can always turn out to evaluate to null or a number or an object or anything else. Flow had the ambition to be sound, which is honorable but they never accomplished it. TypeScript announced up front that they didn't care about soundness: https://www.typescriptlang.org/docs/handbook/type-compatibil...

Soundness is valuable because it makes it possible to look at the types and reason about the program using them. An unsound type-checker like TypeScript or Flow can still be very useful to human readers if most of the types in a codebase are accurate, but you always have to keep that asterisk in the back of your head.

One very concrete consequence of soundness that it makes it possible to compile the code to fast native code. That's what motivated Dart a few years ago to migrate from an unsound type system to a sound one: https://dart.dev/language/type-system so that it could AOT-compile Flutter apps for speed.

Post reply on HN