3 ms·
I 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. I
by RussianCow 2y ago
I 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. :)