Typescript version: 5.6.2
Can someone explain why the ApprovalType
is being modified into specific values of ApprovalType
even when the function parameter type is a conditional type? This happens even if I explicitly pass in the generic parameter <ApprovalType>
when calling the function.
type ApprovalType = "PENDING" | "APPROVED" | "REJECTED";
type A<T> = {
options: T[];
};
type B<T> = {
options: T[];
};
function conditionalTypeFn<T>(props: T extends string ? B<T> : A<T>) {
return props;
}
conditionalTypeFn<ApprovalType>({
/**
* ❌ Type '("PENDING" | "APPROVED" | "REJECTED")[]' is not assignable
* to type '"PENDING"[] | "APPROVED"[] | "REJECTED"[]'
*/
options: ["PENDING", "APPROVED", "REJECTED"],
});
function unionTypeFn<T>(props: A<T> | B<T>) {
return props;
}
unionTypeFn<ApprovalType>({
/* ✅ no error */
options: ["PENDING", "APPROVED", "REJECTED"],
});