When defining functions in TypeScript, is the following approach — defining event types in a separate types.ts
file and then declaring functions as const
with that type — considered a recommended practice in modern development?
Example Code (feels a bit cumbersome to me)
types.ts:
export type MouseEventHandler = EventHandler<MouseEvent>;
event-listeners.ts:
export const onClickCreateItem: MouseEventHandler = async (event, deps) => {
// some logic
};
Current Code (what I usually do)
event-listeners.ts:
export async function onClickCreateItem(
event: MouseEvent,
deps: CreateItemDependencies
) {
// some logic
}
Notes
- I’m fairly new to TypeScript, and my impression is: “Wait, do I really need to set up this much boilerplate just for types?”
- If the answer is basically “Yes, that’s just how TypeScript is, and it’s considered good practice,” then I’ll happily follow that approach.
- I asked ChatGPT, but its answers vary depending on how I phrase the question — sometimes it says one approach is recommended, sometimes the other. So I’d love to hear opinions from real developers here.
Thanks in advance!