4 ms·
People arguing that type systems like TypeScript are useful, because even if they don't catch all errors, they still catch the most common ones. The most commo
by k__ 9y ago
People arguing that type systems like TypeScript are useful, because even if they don't catch all errors, they still catch the most common ones.
The most common JavaScript error I encounter is accessing fields of undefined objects.
Can't we simply have a type-system for JavaScript that only prohibits undefined field access and nothing else?
If we accept that the system will be unsound anyway, why not focus on the main benefit and throw all the baggage out?
- kuon 9y agoTry elm, no more undefined.
- lisper 9y ago> Can't we simply have a type-system for JavaScript that only prohibits undefined field access and nothing else? Yes, but it's not easy for cultural reasons. In order to detect undefined field access you need to know what the fields are at compile time, which is to say, you need classes. Javascript's original design was based on prototypes, which have a fundamental "impedance mismatch" with classes. It's possible to layer classes on top of prototypes. That's actually not so hard, you can find a lot of user-level implementations of classes out there (e.g. https://github.com/neverblued/js-clos https://github.com/neverblued/js-clos). But to make that part of the language so that compilers will be aware of it you'd have to get the community to agree on a design, and that is very challenging. But it's a political problem, not a technical one.
- seanmcdirmid 9y ago> The most common JavaScript error I encounter is accessing fields of undefined objects. TypeScript has optional null checking if you want it. Given that it supports union types, null/undefined tracking fit nicely into the language.
- k__ 9y agoCan I type stuff as any but still non nullable?
- seanmcdirmid 9y agoI think any in TypeScript's context means "not in the type system", and all type rules bounce off it. So if k's type is any, and you type k.foo, it doesn't bother checking it. I assume the same applies to any null checking.
- k__ 9y agoThe "object" type is probably what I was searching.
- spiralx 9y agoThe object type is the type of all non-primitive types, what you want is the Object type instead. Yes, it's confusing, especially as there's also the {} type! This article goes over the differences: https://blog.mariusschulz.com/2017/02/24/typescript-2-2-the-object-type https://blog.mariusschulz.com/2017/02/24/typescript-2-2-the-...