4 ms·
Object.keys() is always frustrating. Also, the array access not returning an optional type (I wish there was a strict option for that) often gets me. We need
by ricksharp 6y ago
Object.keys() is always frustrating.
Also, the array access not returning an optional type (I wish there was a strict option for that) often gets me.
We need a middle ground: “TypeStrict“ that can clean up some of the edge cases.
If you need strict integers, you can create your own fake type:
type Int32 = number & {__type:'Int32'};
I do this with strings sometime when I want a string subtype that can’t be accidentally assigned by a normal string.
- inbx0 6y agoGood news, safe array access is coming in TS 4.1 https://devblogs.microsoft.com/typescript/announcing-typescript-4-1-beta/#no-unchecked-indexed-access https://devblogs.microsoft.com/typescript/announcing-typescr...
- williamdclt 6y agoFor anybody interested in this technique: the googleable term is "branding". It's bringing some nominal typing into TS's structural typing
- svieira 6y agoIn TS 4.1+ there _is_ an option for strict array access called `noUncheckedIndexedAccess` [1] [1]: https://devblogs.microsoft.com/typescript/announcing-typescript-4-1-beta/#no-unchecked-indexed-access https://devblogs.microsoft.com/typescript/announcing-typescr...
- ricksharp 6y agoAwesome! I was just searching for that and all I could find were old github issues saying it was working as intended. Thank you!