News for hackers. Stuff that matters.
Re:TIL: `satisfies` is my favorite TypeScript keyword by kennywinker on Sunday November 23, 2025 @01:24AM (#46021264)
Casting? Not really - i think you’d only need a couple type checks.

Imo this is mostly useful for situations where you want to handle input validation (and errors) in the UI code and this function lives far away from ui code.

Your point about clamping makes sense, and it’s probably worth doing that anyway, but without it being encoded in the type you have to communicate how the function is intended to be used some other way.

Re:TIL: `satisfies` is my favorite TypeScript keyword by samdoesnothing on Sunday November 23, 2025 @02:20AM (#46021488)
How would you convert a Number type to a ClampedNumber type without casting?
Re:TIL: `satisfies` is my favorite TypeScript keyword by kennywinker on Sunday November 23, 2025 @03:29AM (#46021770)
Ah, yeah you’re right. I somehow thought typescript could do type narrowing based on checks - like say:

If (i >= 1) { // i’s type now includes >= 1 }

But that is not the case, so you’d need a single cast to make it work (from number to ClampedNumber<1,200>) or however exactly you’d want to express this.

Tbf having looked more closely into how typescript handles number range types, I don’t think I would ever use them. Not very expressive or clear. I think I hallucinated something closer to what is in this proposal: https://github.com/microsoft/TypeScript/issues/43505

I still think that the general idea of communicating what acceptable input is via the type system is a good one. But the specifics of doing that with numbers isn’t great in typescript yet.

Re:TIL: `satisfies` is my favorite TypeScript keyword by samdoesnothing on Sunday November 23, 2025 @04:09AM (#46021943)
How would you implement it in other languages that support it better? Can you literally just do a range check and the compiler infers its range for types? If so thats actually pretty neat.
Re:TIL: `satisfies` is my favorite TypeScript keyword by skydhash on Sunday November 23, 2025 @08:30AM (#46023422)
What about documentation as in docstring, function signature label and the like. I quite like CommonLisp, EmacsLisp, Go for that.
Re:TIL: `satisfies` is my favorite TypeScript keyword by kennywinker on Sunday November 23, 2025 @07:49PM (#46029039)
Yeah, that’s probably the best possible implementation outside of the type system. The issue with docstrings is that they aren’t checked. So you’ve communicated the api to the coder, and maybe even to the ide, but not to the compiler.