News for hackers. Stuff that matters.
Re:TypeScript: Branded Types by munk-a on Wednesday April 24, 2024 @06:51PM (#40150845)
Isn't this just the classic issue of inferred typing coming back to bite us in the way everyone originally predicted? Go runs into the same issue where wildly different types may be considered the same based purely on coincidental naming and matching against interfaces the original authors had no intent to match against. At the end of the day I think the easier system to work with is one in which all type compatibility needs to be explicitly declared - if your array is iterable defining that it implements iterable lets your compiler shout at you if iterable suddenly gets another abstract method that you didn't implement - and it makes sure that if you add a method `current` to a class it doesn't suddenly means that it properly supports iterable-ity.

Determining types by what things appear to do instead of what they state they do seems like a generally unnecessary compromise in typing safety that isn't really worth the minuscule amount of effort it can save.

Re:TypeScript: Branded Types by skybrian on Wednesday April 24, 2024 @08:27PM (#40151694)
I haven’t run into interfaces coincidentally matching in Go. Have you? It might happen more easily for primitive types, though, and Go does have a way to declare new types:

   type UserId string
Re:TypeScript: Branded Types by klysm on Thursday April 25, 2024 @03:03AM (#40154377)
Are these simple aliases or nominally distinct types?
Re:TypeScript: Branded Types by yencabulator on Thursday April 25, 2024 @11:06AM (#40158453)
Re:TypeScript: Branded Types by Aeolun on Wednesday April 24, 2024 @09:09PM (#40152032)
I mean, they have to stay backwards compatible with Javascript. That kind of limits your options.
Re:TypeScript: Branded Types by munk-a on Thursday April 25, 2024 @02:19PM (#40161141)
Not particularly - they could use distinct tokens (i.e. these brandings) on all types implicitly.
Re:TypeScript: Branded Types by cageface on Wednesday April 24, 2024 @11:39PM (#40153141)
The other problem I've found with structural types in Typescript is that it can lead to some very complex type specifications. Instead of carefully thinking about and naming the entities in your system you can get some very hard to parse type spaghetti.