3 ms·
I believe that typescript type system is so flexible, powerful and complex just because it had to be adapted and built around the shortcomings and limitation of
by yulaow 4y ago
I believe that typescript type system is so flexible, powerful and complex just because it had to be adapted and built around the shortcomings and limitation of javascript. It makes no sense to have something like it if you build a language from the ground up (or if you could just scrap backward compatibility in a bad designed one)
- gernb 4y agoI think I disagree (though happy to be corrected). It's common to have dervied types (example Base = Shape, Derived = Circle, Rectangle, Hexagon). Often you have a collection of Shape and often there is some runtime code that can go from a Base type to a Dervied type based on some key or value but the relationship between the key and the Dervied type is rarely directly expressed in the type system where as in typescript it can be. I think an example is how typescript can know, based on the first argument to a listener, what the Event type coming in will be elem.addEventListner('mousedown', (foo) => {...}); elem.addEventListner('keydown', (bar) => {...}); typescript knows foo is a MouseEvent and bar is a KeyboardEvent Of course you could argue that `addEventListener` is just bad design but I feel like there are legit uses to being able to associate an enum or string with type and I haven't seen that feature in other languages I've used.
- int_19h 4y agoKeeping in mind that it can only associate compile-time-known values, there's no practical advantage over something like a tagged union.
- adamddev1 4y agoI've had to work with some really complex data types and I have really appreciated the utility types, conditional types, modifying data type recursively, template literal types etc. I was wondering how many other types languages are able to do all that stuff.
- curtisf 4y agoNo, TypeScript has these type system features because they are necessary to accurately type the programs people actually write using JavaScript. This isn't because of the shortcomings of JavaScript, it's because of the shortcomings of simple type systems. Simple type systems (like those of Java or Go) restrict the kinds of programs you can easily write while satisfying the type checker. Normally, the alternative is resorting to languages without static type checkers, like JavaScript. TypeScript allows mostly idiomatic JavaScript even with complex runtime invariants to be given quite accurate types at compile time. It has features I miss all the time in Java.