by codeflo · 3y ago · 0 comments
const base: BaseClass = new SubClass();
const keys = Object.keys(base); // should give you the subclass keys as well
(This is pseudo-code that might not quite work due to how prototypes and classes interact, but I'm trying to illustrate a principle.)What's perhaps a bit surprising is that all types in TypeScript behave like BaseClass in this example. Object types (e.g. {a: number, b: string}) specify the minimally required properties, not the maximally allowed properties -- they are effectively the same thing as interfaces. Any concrete object that implements the interface can have more properties than the interface requires.
That's so fundamental to the type system that I think if you want to work around it, you might not fully understand how TypeScript works yet.
I think most engineers who use (and swear by) TypeScript don't fully understand how TypeScript works (or JavaScript for that matter).
I've worked across multiple teams at multiple companies and always run into types I can't trust because they are just plain wrong and inconsistent with the runtime, or data that isn't typed correctly. I also found that everyone writes TS differently, and sometimes the verbose typings and weird non-idiomatic patterns introduced just to please tsc actually make the code harder to read (30-line JS modules become 120-line TS behemoths). I still prefer working with TS on teams (though I prefer JS on solo projects now), but after a while I ask "is this the best we can do?"
/end minirant
In contrast, OCaml's objects differs <a: int; b: int> (exactly those fields) from <a: int; b: int; ..> (may have more fields). Likewise for Purescript records etc
This stuff was figured out decades ago. There was no need for Typescript to be like this
typescript is strictly a superset of javascript, and javascript is like this.
I think it is also a large part of the reason why TypeScript is amazing and very useful in the real world.
I know this comes across as really hostile but I just feel very burned by TS's promises and hype. You can work in it more confidently than JS I think but a lot of that is due to the tooling. In exchange you massively increase the required knowledge and mental modeling to keep track of what's going on and why. I used to be all about it but now after using it in a few big projects I've lost the enthusiasm.
Someone on here once described it in an excellent way, saying it's great for working out "type puzzles," which smart engineers tend to experience as important and productive. But the actual practical benefits in "the real world" as you say I'm becoming less and less convinced of.