3 ms·
What you say is true, but after years of working with TypeScript (and about 15 years of Java before that) I'd say that from a purely practical perspective the s
by hn_throwaway_99 2y ago
What you say is true, but after years of working with TypeScript (and about 15 years of Java before that) I'd say that from a purely practical perspective the structural typing approach is much more productive.
I still have PTSD from the number of times I had to do big, painful refactorings in Java simply because of the way strong typing with nominal types works. I still shudder to think of Jersey 1.x to 2.x migrations from many years ago (and that was a PITA for many reasons beyond just nominal typing, but it could have been a lot easier with structural types).
I love branded types (and what I think of their close cousin of string template literal types in TS) because they make code safer and much more self-documenting with minimal effort and 0 runtime overhead.
- RussianCow 2y agoI think proper type inference along with some sort of struct literal syntax could make a lot of these problems go away without the need for structural typing. If you can create structs without explicitly mentioning the type (as long as it can be inferred), then you can use the same syntax for different types as long as their signatures are the same. Structural typing makes it too easy to accidentally pass something that looks like a duck but is actually a goose. As an example, this syntax should be possible without structural typing or explicitly specifying the type of the value: // assuming a type signature of moveToPoint(Point) moveToPoint({x: 5, y: 10}) // the struct literal is inferred to be a Point I believe F# has syntax like this, but it's been a while since I've used it so I don't remember the details.
- hn_throwaway_99 2y ago> Structural typing makes it too easy to accidentally pass something that looks like a duck but is actually a goose. This may be true, but in reality after 7 years of using TypeScript I don't think I've ever encountered this as a bug for an object (Record) type. Even for branded types I find the value much more in self-documenting code than actual type safety, and I only have used branded types for primitives.
- sapling-ginger 2y agoAll valid javascript programs are valid typescript programs, so typescript necessarily must have structural type, it cannot have nominal type without breaking compatibility with javascript, which is a hard requirement.
- RussianCow 2y agoCorrect. My comment was about programming languages in general and not specific to TypeScript. :)
- cageface 2y agoOverall I do like working with Typescript and structural typing does have some real advantages but, like just about everything in engineering, there are also tradeoffs. Without some discipline you can create some very messy type spaghetti with TS. Nominal typing is more rigid but it can also force you to think more clearly about and carefully define the different entities in your system.