When I define a function type with parameters like value: string | number
, I am able to define a function (value: string) => null of that type without TypeScript showing any warnings.
However, if I define a function type with a generic parameter like
<T extends string | number>(value: T)
, TypeScript will throw an error.
Can someone explain this behavior?
Code example:
type OnChangeUnion = (value: string | number) => void;
type OnChangeGeneric = <T extends string | number>(value: T) => void;
const handleChange = (value: string) => null;
const onChange: OnChangeGeneric = handleChange;
const onChangeA: OnChangeUnion = handleChange;