3 ms·
As a long time TypeScript user, I do think this is a design mistake with the language. For example, it breaks instanceof checks. However: - The language prima
by paavohtl 7y ago
As a long time TypeScript user, I do think this is a design mistake with the language. For example, it breaks instanceof checks.
However:
- The language primarily uses structural typing. If two classes are compatible, it's not completely unreasonable to expect them to also behave in a structural way (though I would prefer them not to).
- Adding a private field to a class makes it work as a nominal type (https://michalzalecki.com/nominal-typing-in-typescript/#approach-1-class-with-a-private-property https://michalzalecki.com/nominal-typing-in-typescript/#appr...). It feels a bit hacky and it's not very well documented, but you could argue that until a class has some private state you can't publicly access, there's no reason to prevent you from declaring a compatible class.
- From my experience idiomatic TypeScript rarely uses classes, the primary exception being React components. In teams I've been a part of we've always written TypeScript (and JavaScript) like an (impure) functional language, taking full advantage of ADTs, simple structurally typed data and more-or-less pure functions.
- The_rationalist 7y agotaking full advantage of ADTs What? There is no clean way to switch on the types of an ADT on typescript, I believe.
- alexcole 7y agoWell discriminated unions are TypeScript's implementation of ADTs (but whether they are "clean" is could be debated)