Re:TypeScript: Branded Types
by eyelidlessness on Wednesday April 24, 2024 @03:16PM
(#40148363)
I mean. I think this specific case is a lot more principled than you seem to think. It’s certainly well reasoned from a perspective of structural typing by default.
[ Parent
| on HN ]
Re:TypeScript: Branded Types
by mirekrusin on Wednesday April 24, 2024 @05:21PM
(#40149837)
Yes, they have their reasons. They always do, tradeoff etc, I know.
It doesn't change the fact that ie. adding private member is breaking change in your library which is kind of funny (until it's not funny of course).
Also stuff like:
class Foo { private foo = 1 }
class Bar {}
const a: Bar = new Foo
...typechecks so that's it for nominality.It's all ifs all the way down.
Re:TypeScript: Branded Types
by eyelidlessness on Wednesday April 24, 2024 @05:45PM
(#40150111)
It’s only a breaking change if you’ve been using a class to stand in for an interface (happens to the best of us I’m sure!). You can still avoid it being one after the fact by introducing a non-breaking interface consistent with what you were already accepting/expecting.
And yeah, I’m not a fan of that class instance assignability case. Not to make excuses for it, but I have other reasons I generally prefer to expose interfaces (distinct from classes, even if they’re identical in shape and even have the same internal purpose) at most API boundaries; avoiding that particular footgun just turns out to be a nice happy side effect of the preference.