TypeScript tip: If a type is only used by one file, colocate the type with...


TypeScript tip: If a type is only used by one file, colocate the type with the file.

This clarifies the close relationship, reduces file navigation overhead, and makes it easy to update the type and code at the same time.
Note that this rule is just like any other file or function.

I colocate when I can. I centralize when I must.
When the type is only used once, it's ideally placed inside the file where it's called.

One exception: If the type is really long, it may be preferable to extract the type to a separate file with a corresponding name, in the same dir:

user.tsx
user.types.tsx

View original on X