| ▲ | samdoesnothing 3 hours ago | |||||||
How would you convert a Number type to a ClampedNumber type without casting? | ||||||||
| ▲ | kennywinker 2 hours ago | parent [-] | |||||||
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. | ||||||||
| ||||||||