Re:Hegel – An advanced static type checker for JavaScript
by TheRealPomax on Thursday October 13, 2022 @11:47AM
(#33192613)
Hmm...
The very first example is already an odd choice, given that <number> is type-compatible with <string|number>. Errors should be flagged once we try to call functions on the elements, with calls on number[...] validated based on <number>, and calls on numbersOrStrings[...] validated based on the shared API between <string> and <number> (which is a tiny API surface, but there is one).
Why is this not an error based on the incorrect assignment we already had? "HegelError: cannot assign to alias numberOfStrings of numbers, numbers is of type <number>".
Heck even this error is weird: we declared numbers as an array of <number>, not an array of <number|undefined>. If you're going to be that strict, track list sizes and go "HegelError: Property numbers[1] is undefined, which is type-incompatible with <number>".
[ Parent
| on HN ]
Re:Hegel – An advanced static type checker for JavaScript
by lshevtsov on Thursday October 27, 2022 @09:18AM
(#33357037)
It's because it is not proven that element with the index 1 exists. Even TypeScript enforces bounds checking these days.
As for the first one - as i understand, this will spoil the original array, as it is assigned by reference; so by writing the string into `numbersOrStrings`, you also write it into `numbers`, thus violating the type.