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.
`satisfies` is my favorite TypeScript keyword (2024)
101–110 of 217 posts
Re: `satisfies` is my favorite TypeScript keyword (2024)
#102Earlier quoted context omitted.
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.
The fact that there can be runtime type errors that were proven impossible at compile time is why I will never enjoy TypeScript.
Re: `satisfies` is my favorite TypeScript keyword (2024)
#103Earlier quoted context omitted.
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.
The fact that there can be runtime type errors that were proven impossible at compile time is why I will never enjoy TypeScript.
Re: `satisfies` is my favorite TypeScript keyword (2024)
#104Earlier quoted context omitted.
I recently had to write a Promise.all, but using an object instead of an array. That was... non-trivial.
If it's what I'm thinking, that one isn't too bad. I wrote it awhile back: export async function promiseAll >>(promises: T): Promise }> { const keys = Object.keys(promises) as Array ; const result = await Promise.all(keys.map(key => promises[key])); return Object.fromEntries(result.map((value, i) => [keys[i], value])) as { [K in keyof T]: Awaited };
Without internet or AI I wouldn't attempt writing anything like that.
Re: `satisfies` is my favorite TypeScript keyword (2024)
#105> 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…
Re: `satisfies` is my favorite TypeScript keyword (2024)
#106> 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…
Tell me when Go is browser ready
export GOOS=js
export GOARCH=wasmRe: `satisfies` is my favorite TypeScript keyword (2024)
#107Earlier quoted context omitted.
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.
The fact that there can be runtime type errors that were proven impossible at compile time is why I will never enjoy TypeScript.
The "impossibility" is just a trait of the type definitions and assertions that developers specify. You don't need to use TypeScript to understand that impossibilities written in by developers can and often are very possible.
Re: `satisfies` is my favorite TypeScript keyword (2024)
#108Earlier quoted context omitted.
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)
#109> 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…
You're right, but that begs the question: does a type system really require such complexity? I'm aware that type theory is a field in and of itself, with a lot of history and breadth, but do developers really need deep levels of type flexibility for a language to be useful and for the compiler to be helpful? I think TypeScript encourages "overtyping" to the detriment of legibility and comprehension, even though it is…
Re: `satisfies` is my favorite TypeScript keyword (2024)
#110> 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…
I think you're both exaggerating your blanket accusations of incompetence and confusing learning curve with mastering extremely niche techniques akin to language gotchas.