Hacker News new | ask | show | jobs
by erikpukinskis 1478 days ago
IMO reusing types is an anti-pattern. It leads to knots of functions which are all tightly coupled since they “use the same types” even though they actually only need small subsets of those types. Changing one of these functions will then often break several of the conjoined functions.

When you start to hear people say of their PRs “it works but I just have to fix the types”, that usually means the codebase is trying to re-use too many types. In my experience.

The beauty of TypeScript is that you can have two different very narrowly specified types and the compiler will tell you if they’re compatible or not.

Reusing types is throwing away the most valuable feature of TypeScript.

For me, the type signature is part of the function signature and therefore should not be re-used.

Imagine what a disaster it would be if function signatures were reusable:

    sig UserDatabase(user, db)

    create: UserDatabase =>
      db.write(“users”, user)

    update: UserDatabase =>
      db.get(“users”, user.id).update(user)

It’s too much. DRY can be taken too far.
2 comments

It's not an anti-pattern to reuse types, it's an anti-pattern to apply something you don't need or apply too much of it, which is exactly what you are talking about. There is of everything a "too much". It's up to you how and in which part of your app you will use that. The difference is: you have the opinion to do so now where it makes sense. Nobody says you have to reuse everything up to a point where it becomes counterproductive. Having the option vs not having the option. It's definitely better in my opinion than being forces to even learn new inflexible languages/APIs to describe your model like JSON-schema, decorators, openAPI json, GraphQL, Prisma, ... - whether you reuse is up to you.
yes yes yes! this is so true