Re:Hegel – An advanced static type checker for JavaScript
by TheRealPomax on Wednesday October 12, 2022 @07:04PM
(#33184940)
I understand why marketing would insist, but "Hegel attempts to prevent runtime TypeErrors with a strong type system" sounds like "we haven't been paying attention to the JS landscape for the last few years and didn't know TypeScript already existed"? And if it's just a better TS tool chain... that'd be worth calling out.
[ Parent
| on HN ]
Re:Hegel – An advanced static type checker for JavaScript
by andrewmcwatters on Wednesday October 12, 2022 @07:06PM
(#33184958)
Re:Hegel – An advanced static type checker for JavaScript
by TheRealPomax on Thursday October 13, 2022 @11:47AM
(#33192613)
Hmm...
const numbers: Array<number> = [];
// HegelError: Type "Array<number>" is incompatible with type "Array<number | string>"
const numbersOrStrings: Array<string | number> = numbers;
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). numbersOrStrings[1] = "Hello, TypeError!";
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>". // HegelError: Property "toFixed" does not exist in "Number | undefined"
numbers[1].toFixed(1);
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>".
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.
Re:Hegel – An advanced static type checker for JavaScript
by sergiomattei on Wednesday October 12, 2022 @07:06PM
(#33184957)
I thought the same thing, but they have a page that explains it pretty well — TypeScript v. Hegel
Turns out Hegel makes it a goal to be provably correct in respect to types, whereas TypeScript makes tradeoffs for productivity.
Re:Hegel – An advanced static type checker for JavaScript
by cercatrova on Wednesday October 12, 2022 @09:01PM
(#33185894)
The benefit is right there in the line you quoted. TypeScript doesn't offer type safety at runtime, Hegel apparently does.
Re:Hegel – An advanced static type checker for JavaScript
by TheRealPomax on Thursday October 13, 2022 @11:43AM
(#33192555)
Then maybe read the line again, because it very much does not say that =)
Re:Hegel – An advanced static type checker for JavaScript
by vore on Wednesday October 12, 2022 @09:58PM
(#33186266)
The line says no such thing, in fact it says the opposite. From the project page on GitHub, Hegel wants to have zero runtime type checking and have it all be proved statically at compile time.
Re:Hegel – An advanced static type checker for JavaScript
by cush on Thursday October 13, 2022 @12:27AM
(#33187240)
You're reading type safety as type checking
Re:Hegel – An advanced static type checker for JavaScript
by vore on Friday October 14, 2022 @12:55AM
(#33199787)
Then what does "type safety at runtime" even mean? TypeScript is surely also type safe at runtime then, even if the type system is unsound: that is, after all, its whole value proposition.