Live data from Hacker News

Node.js adds experimental support for TypeScript

github.com

411–420 of 570 posts

Re: Node.js adds experimental support for TypeScript

#411

Earlier quoted context omitted.

> and in that case there's nothing unsound about ts since it won't allow you to do so Consider this example ( https://www.typescriptlang.org/play/?ssl=10&ssc=1&pln=1&pc=1... ): function messUpTheArray(arr: Array ): void { arr.push(3); } const strings: Array = ['foo', 'bar']; messUpTheArray(strings); const s: string = strings[2]; console.log(s.toLowerCase()) Could you explain how this isn't the type system accepting t…

That's a good example, albeit quite of a far-fetched one. In Haskell land, where the type system is considered sound you have `head` functions of type `List a -> a` that are unsound too, because the list might be empty.

That option also exists, you can just leave out the `messUpTheArray` lines and you get an error about how `undefined` also doesn't have a `.toLowerCase()` method.

However this problem as stated is slightly different and has to do with a failure of OOP/subtyping to actually intermingle with our expectations of covariance.

So to just use classic "animal metaphor" OOP, if you have an Animal class with Dog and Cat subclasses, and you create an IORef, a cell that can contain a cat, you would like to provide that to an IORef function because you want to think of the type as covariant: Cat is a subtype of Animal, F should be a subtype of F. The problem is that this function now has the blessing of the type system to store a Dog in the cell, which can be observed by the parts that still consider this an IORef.

Put slightly differently, in OOP, the methods of IORef all accept an implicit IORef called `this`, if those methods are part of what define an IORef then an IORef is necessarily invariant, not covariant, in . And then you can't assume subtyping. So to be sound a subtype system would presumably have to actually mark contra/covariance around everything, and TypeScript very intentionally documents that they don't do this and are just trying to make a "best effort" pass because JavaScript has 0 types, and crappy types are better than no types, and we can't wait for perfect types to replace the crappy types.

Re: Node.js adds experimental support for TypeScript

#412

Earlier quoted context omitted.

Types as comments is what jsdoc supports. Flow is compiled like typescript. tsc does also support jsdoc though, so technically I think tsc is closer to supporting types as comments, though it's possible Flow has this also.

https://flow.org/en/docs/types/comments/

Thanks, I've never used flow and didn't know this. I've only seen it used in the non-comment form.

Re: Node.js adds experimental support for TypeScript

#413

Earlier quoted context omitted.

Why are you still transpiling to .cjs in 2024? ESM is supported in every LTS version of Node now. We can kill CJS, we have the power.

Because I don't like breaking things unnecessarily. Some of my libraries are 10 years old and depended upon by similarly old projects that are not using ESM and probably never will. Besides, it's already going through one transpilation step to go from TS to ESM, so adding a second one for CJS really isn't that much hassle. I think if node.js had made require() work with ESM, I could probably drop CJS. But since that'…

> adding a second one for CJS

Nobody is arguing for that. Once you ship ESM, you can continue shipping ESM.

In Node 22 you can even require() ES modules (with an experimental flag, at the moment)

Re: Node.js adds experimental support for TypeScript

#414
post #43

Earlier quoted context omitted.

Yes, that’s explaining what type erasure is. “type as comments” is what Flow supports, literal comments for type annotations. I think we disagree on the terminology but agree on the goal of the proposal

Types as comments is what jsdoc supports. Flow is compiled like typescript. tsc does also support jsdoc though, so technically I think tsc is closer to supporting types as comments, though it's possible Flow has this also.

Types _in_ comments is different from types _as_ comments. "Types as comments" refers to how type expressions are parsed.

Re: Node.js adds experimental support for TypeScript

#415

Earlier quoted context omitted.

i tried that but had to revert to vitest, the native test runner feels incomplete atm.

What is missing for your use case or workflow?

Probably a bunch of assertion types and general DX. Node:test is just a feature, Vitest is a whole product. The former might be enough for small packages but nowhere near useful for anything non-trivial.

Re: Node.js adds experimental support for TypeScript

#416

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…

Imho standardizing the syntax is enough and this is what https://github.com/tc39/proposal-type-annotations does. The type checker is immensely complex and should be left out so that other type checkers can be developed, e.g. similar to how it's for Python today.

That doesn't standardize the syntax, only types-as-comments. It would have to standardize some delimiters for type expressions, but that's it.

I do think that the semantics should be standardized too, otherwise you have non-interoperable types. The goal should be that you can use two libraries together without having to make sure they use the same type-checker.

Re: Node.js adds experimental support for TypeScript

#417

I beg of thee, do not do this. I get that people love typescript but I am already running into a problem where javascript resources are written in typescript by default with nothing for regular javascript. This is the same problem that happened when JQuery hit its peak popularity and an overwhelming amount of resources and guides amounted to "Oh just do this in JQuery"

Tell me you're out of touch without telling me that you're out of touch. jQuery was so popular because writing anymore than a few lines of vanilla JavaScript was an *awful* experience due to all differences in browsers. When things eventually standardized-ish and jQuery became unnecessary, other libraries/ecosystems popped up (e.g. React/JSX) to make writing webapps easier because writing anymore than a few lines of…

JavaScript is not terrible and I don’t understand where you got that from. Since ES2015 came out, it’s actually rather pleasant.

Re: Node.js adds experimental support for TypeScript

#419
post #320

Side note, but IMO Typescript is too complicated. They should have stuck to a reasonably simple type system but now I see projects with incomprehensible and frankly unmaintainable typescript consisting of extremely complex generics, type conditionals, and type constraints. Basically if you aren't careful you'll find your project metaprogramming in typescript's turing complete meta language...

TypeScript in its current usage reminds me of Hello, World! or FizzBuzz Enterprise Edition. There's almost more code dedicated to typing than the actual running software itself in some codebases I've seen. The authors trick you with reasonable examples on https://www.typescriptlang.org , but in the wild, you have these ridiculous codebases that couldn't control themselves and they have this insane ratio of multiple d…

I also get lost in types sometimes, but the point of types is that they help you. Typescript lets you just use `as any` and `as unknown` as you please, but you want complex constraints you will need complex types.

There are some type libraries that parse GraphQL queries and CSS selectors. They’re crazy to look at but they’re hugely helpful.

Re: Node.js adds experimental support for TypeScript

#420

Earlier quoted context omitted.

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…

Paired characters... That's an interesting topic. Do you happen to know if UTF-8 contains more "pair" characters? In Latex we call these delimiteres, but that's just my limited experience coming in from math side. I tend to agree that it would be helpful to have more kind of nesting/pairing/grouping/delimiting characters. The problem is my imagination is limited to what I know from the ASCII world, and so it goes...…

So many different pairs are available. I like Asian corner brackets 「」and French guillemets « ». The angle brackets 〈〉 are popular in CS/math papers I think, though they might be confused with .
Post reply on HN