4 ms·
TypeScript's types are structural, and I don't consider this to be a good thing. It's useful for JavaScript interop, yes, but just because two values have the s
by yeputons 5y ago
TypeScript's types are structural, and I don't consider this to be a good thing. It's useful for JavaScript interop, yes, but just because two values have the same representation and method names does not mean they're interchangeable. Case in point: time units.
So while there may be some use of structural subtyping, I don't think it's codebase-wide.
- grumpyprole 5y agoHere's a counter example. I've seen numerous Java projects define a nominal predicate type, for example com.google.common.base.Predicate. None of these are interoperable without writing adapter code. ML and Haskell would essentially use a structural type: t -> bool Much simpler and easier to consume. How would you assign a nominal type to the result of a SQL query? So there are arguments on both sides. My point was that structural types can always easily be turned into a nominal type, but not the other way around. Therefore they should be the default.
- de_keyboard 5y agofunction types are structural in most nominally-typed languages I have used, perhaps a poor example?
- grumpyprole 5y agoAre you sure? What language are you referring to? For example, although Java method definitions are essentially defined in terms of tuples, function classes and objects in Java are all entirely nominal.
- _old_dude_ 5y agoThere are interoperable since Java 8, using the operator :: com.google.common.base.Predicate<String> guavaPredicate = ... java.util.function.Predicate<String> predicate = guavaPredicate::apply;
- Twisol 5y agoThe :: syntax minimizes the pain, but the fact you have to do it at all is still irritating, and IIUC it incurs a small runtime cost due to the intermediate object that's generated to convert between the two interfaces. I recently cleared out some of these intermediaries in a hot area of code and got a small but significant performance boost.
- cageface 5y agoAfter working in nominally typed languages for years I’m finding Typescript’s approach to types very liberating. I think structural typing with the option of defining nominal types is a sweet spot in type ergonomics.
- cwalv 5y agoI agree. I think part of the reason for this is that Typescript's type system was created with a goal of expressing the APIs of many existing js APIs, and many of those APIs are designed without any real constraint imposed by the type system.
- sullyj3 5y agoRight, there should be the ability to use either structural or nominal as appropriate.