it's possible to compile code with this intent:
type Tick = Date | number;
type Unary<T, R> = (_: T) => R;
function formatter<T extends Tick>(tick: T): Unary<T, string> {
if (tick instanceof Date) {
return (date: Date) => date.toUTCString();
}
return (int: number) => int.toFixed();
}
My idea is that if T
is Date
, tick
is Date
and return type is Unary<Date, string>
, but Typescript tries it to unify with Unary<T, string>
. It is because T extends Tick
is broader than just Tick
union? If so, can I limit this generic?
formatter(someExpr)
whensomeExpr
is of typeDate | number
?