Hacker News new | ask | show | jobs
by throwaway09432 1187 days ago
Typescript is a good try, but ultimately our favourite JS runtime errors still happen, even with all the strictest ts settings. Syntactically it is quite clumsy, you can see the uninspired mix of C++ and Java in there. The typings now although people are doing lots of cute things with them, don't do some really fundamental things. For example there is currently no sensible way to type a non empty array in Typescript. Also the discriminated union with the identifying string literal in there is not terribly elegant.

Personally I wish either Rescript or Purescript gained adoption instead.

1 comments

> there is currently no sensible way to type a non empty array in Typescript

If we remove the "sensible" requirement:

    type NonEmptyArray = [any, ...any[]]
Or better:

    type NonEmptyArray = [unknown, ...unknown[]];
    const nea: NonEmptyArray = [];
    //    ^^^ Type '[]' is not assignable to type 'NonEmptyArray'.
    //        Source has 0 element(s) but target requires 1.
Can you map over this and preserve the NonEmptyArray type?
Do you mean pass in a type?

    type NonEmptyArray<T> = [T, ...T[]];
I mean if I have a NonEmptyArray say xs, and I then xs.map(x=>2*x), will the result of this expression be a NonEmptyArray ?
No, unfortunately it will just be number[] as the return type.