Relative Content

Tag Archive for typescriptunion-types

How to get typescript to be more specific in string interpolation over a union

export const schedules = [ { unit: “hour”, quantity: 6 }, { unit: “day”, quantity: 1 }, ] as const; const getOffsetName = (schedule: (typeof schedules)[number]) => `${schedule.quantity}_${schedule.unit}` Why is the return type of getOffsetName “6_hour” | “6_day” | “1_hour” | “1_day” instead of “6_hour” | “1_day”? Is there any way to make typescript more […]

How to handle parameters of union functions correctly when they are tied to another parameter?

In typescript I know that for type safety when you have a union of functions, if you want to call this function, you have to pass it a intersection of its parameters instead of an union, but this behavior can be annoying when you already have type checks that ensure that no matter what function of the union you’ll call, you will always pass it the right parameters even when passing an union instead of passing every possible parameters. Let me explain with a small example: