Live data from Hacker News

`satisfies` is my favorite TypeScript keyword (2024)

sjer.red

81–90 of 217 posts

Re: `satisfies` is my favorite TypeScript keyword (2024)

#81
post #71
post #48

99% of my use of `satisfies` is to type-check exhaustivity in `switch` statements: type Foo = 'foo' | 'bar'; const myFoo: Foo = 'foo'; switch (myFoo) { case 'foo': // do stuff break; default: myFoo satisfies never; // Error here because 'bar' not handled }

I generally do this via a `throw UnsupportedValueError(value)`, where the exception constructor only accepts a `never`. That way I have both a compile time check as well as an error at runtime, if anything weird happens and there's an unexpected value.

That's great, I'm going to use that one in the future.

Re: `satisfies` is my favorite TypeScript keyword (2024)

#82
> TypeScript is a wonderfully advanced language though it has an unfortunately steep learning curve; in many ways it’s the complete opposite of Go.

Replace "TypeScript" with "C++" and the same can be said.

It is one of the worst languages ever designed and already built on top of a sloppy foundation (Javascript) compared to Go.

The language encourages escape hatches and tons of flexibility on how it checks its types and creates the risk of inconsistency to engineers on which rules to adopt and there is always one engineer that will disagree with some settings and argue to turn on/off a rule to defeat the purpose of the language.

At this stage, its no better than C++ but significantly slower, and I've seen the same mistakes (enums, allowing "as XYZ" casting, etc) in C++ creeping into TypeScript.

Even the entire language parser and type checker is being rewritten in Go. [0]

[0] https://devblogs.microsoft.com/typescript/typescript-native-...

Re: `satisfies` is my favorite TypeScript keyword (2024)

#83
post #71
post #48

99% of my use of `satisfies` is to type-check exhaustivity in `switch` statements: type Foo = 'foo' | 'bar'; const myFoo: Foo = 'foo'; switch (myFoo) { case 'foo': // do stuff break; default: myFoo satisfies never; // Error here because 'bar' not handled }

I generally do this via a `throw UnsupportedValueError(value)`, where the exception constructor only accepts a `never`. That way I have both a compile time check as well as an error at runtime, if anything weird happens and there's an unexpected value.

That's very clever!

Re: `satisfies` is my favorite TypeScript keyword (2024)

#84
post #27

Earlier quoted context omitted.

For those unfamiliar with TS, the above is just... function flat([head, ...tail]) { return Array.isArray(head) ? [...flat(head), ...flat(tail)] : [head, ...flat(tail)] } ...in TS syntax.

Well, it is the type of that, in TS syntax. Few are the statically-typed languages that can even express that type.

Java: List

Python: list[Any]

...what am I missing?

Re: `satisfies` is my favorite TypeScript keyword (2024)

#85

> TypeScript is a wonderfully advanced language though it has an unfortunately steep learning curve An extremely steep one. The average multi-year TypeScript developer I meet can barely write a basic utility type, let alone has any general (non TypeScript related) notion of cardinality or sub typing. Hell, ask someone to write a signature for array flat, you'd be surprised how many would fail. Too many really stop at…

Not steep so much as deep.

There’s a lot you can do in TypeScript. But you don’t have to do it. And TS existed successfully a long time without those features.

Re: `satisfies` is my favorite TypeScript keyword (2024)

#86
post #68

Earlier quoted context omitted.

We don't have to deal in hypotheticals - we have a concrete example here. There's a method, array.flat() that does a thing that we can correctly describe in TypeScript's type system. You say you would reject those correct types, but for what alternative? It's hugely beneficial to library users to automatically get correctly type return values from functions without having to do error-prone casts. I would always take…

There's nothing I can do about the standard JavaScript library, but in terms of code I have influence over, I very simply would not write a difficult-to-type method like Array.prototype.flat(), if I could help it. That's what I mean by an XY Problem - why are we writing this difficult-to-type method in the first place and what can we do instead? Let's suppose Array.prototype.flat() wasn't in the standard library, whi…

> MyStructure -> [MyElements]

Right, from the structure you get an array with one element which is likely an union type from that naming.

Honestly, you sound more like your arguing from the perspective of a person unwilling to learn new things, considering you couldn't even get that type correct.

To begin with, that flat signature wasn't even hard to understand?

Re: `satisfies` is my favorite TypeScript keyword (2024)

#87
post #86
post #68

Earlier quoted context omitted.

There's nothing I can do about the standard JavaScript library, but in terms of code I have influence over, I very simply would not write a difficult-to-type method like Array.prototype.flat(), if I could help it. That's what I mean by an XY Problem - why are we writing this difficult-to-type method in the first place and what can we do instead? Let's suppose Array.prototype.flat() wasn't in the standard library, whi…

> MyStructure -> [MyElements] Right, from the structure you get an array with one element which is likely an union type from that naming. Honestly, you sound more like your arguing from the perspective of a person unwilling to learn new things, considering you couldn't even get that type correct. To begin with, that flat signature wasn't even hard to understand?

Brah, If you have a type with that many characters in it that isn’t a super long string name, it’s not easy to understand unless you are the 1% of 1% when it comes to interpreting this specific language.

On top of that I fully agree with the poster you’re responding to. In general application code that’s and extremely complicated type, generally done by someone being as clever as can be. And if the code you’ve written when you’re being as clever as possible has a bug in it, you won’t be clever enough to debug it.

Re: `satisfies` is my favorite TypeScript keyword (2024)

#88
post #86
post #68

Earlier quoted context omitted.

There's nothing I can do about the standard JavaScript library, but in terms of code I have influence over, I very simply would not write a difficult-to-type method like Array.prototype.flat(), if I could help it. That's what I mean by an XY Problem - why are we writing this difficult-to-type method in the first place and what can we do instead? Let's suppose Array.prototype.flat() wasn't in the standard library, whi…

> MyStructure -> [MyElements] Right, from the structure you get an array with one element which is likely an union type from that naming. Honestly, you sound more like your arguing from the perspective of a person unwilling to learn new things, considering you couldn't even get that type correct. To begin with, that flat signature wasn't even hard to understand?

What I wrote would be a syntax error in TypeScript (no name for the argument, wrong arrow), not a function that returns array with one element; I used Haskell-ish notation instead of TypeScript's more verbose "(structure: MyStructure) => MyElement[]".

I thought it was clear enough that I was being informal and what I meant was clear, but that was admittedly probably a mistake. But to infer an implication from that that I'm "unwilling to learn new things" is a non sequitur and honestly kind of an unnecessarily dickish accusation.

Re: `satisfies` is my favorite TypeScript keyword (2024)

#89

Earlier quoted context omitted.

Honestly I just use TypeScript to prevent `1 + [] == "1"` and check that functions are called with arguments. I don't care about type theory at all and the whole thing strikes me as programmers larping (poorly) as mathematicians.

I couldn't care less about mathematics, but I do care about making impossible state impossible and types documenting the domain. If you type some state as: isLoading: boolean result: Foo hasError: boolean errorMessage: string | null then you're creating a giant mess of a soup where the state of your program could have a result, be loading and an error at the same time. If you could recognise that the state of your pr…

>encoding that a number is between 2 and 200

What’s the point of this level of autism when you still have to add run time checks?

Re: `satisfies` is my favorite TypeScript keyword (2024)

#90

> TypeScript is a wonderfully advanced language though it has an unfortunately steep learning curve An extremely steep one. The average multi-year TypeScript developer I meet can barely write a basic utility type, let alone has any general (non TypeScript related) notion of cardinality or sub typing. Hell, ask someone to write a signature for array flat, you'd be surprised how many would fail. Too many really stop at…

to our defense, we want to build stuff not become ts wizards. also I've worked with libraries with heavy heavy typing that it was a nightmare if you wanted to use their lib in any other way than what they have imagined.
Post reply on HN