Inspired by - https://stackoverflow.com/a/53955431/5039290
type IsUnion<T> = [T] extends [UnionToIntersection<T>] ? false : true;
Use case
Create a specific type that guarantees only unions from a generic type that could be mixed-bag.
type MemberInfo = {
first_name: string;
last_name: string;
date_of_birth: number;
favourite_fruit: 'apple' | 'mango' | 'banana';
industry: 'tech' | 'art' | 'medicine';
}
type ChoiceBasedField = {
[K in keyof MemberInfo]: IsUnion<MemberInfo[K]> extends true ? K : never
}[keyof RespondentFields];
// can accept 'favourite_fruit' or 'industry', but errors on anything else
const renderDropdown = (field: ChoiceBasedField) => (
<select> ... </select>
);
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too