Re:Hegel – An advanced static type checker for JavaScript
by matt_kantor on Thursday October 13, 2022 @01:06AM
(#33187466)
> they are typechecked, so you couldn't have a type guard that claims to return a number but actually returns an array, at least not without bypassing the Typescript checker some other way
Sadly this isn't true: https://tsplay.dev/WvGArw
Re:Hegel – An advanced static type checker for JavaScript
by MrJohz on Thursday October 13, 2022 @01:19AM
(#33187548)
Good point, I wasn't 100% sure on that and should have checked myself. I'd say there's still a lot of value in having type guards as explicit places that can be heavily checked, e.g. with unit tests, to form strong boundaries to the rest of the code you right.
Type assertions match what you describe better - they are ways to override the compiler's opinion about the type of a variable or expression. They are, as you say, unsafe in the sense that the compiler has no way of knowing if this assertion is true or not.