Does anyone know why the code below has a type error even if there is already a if
guard before the code that creates the variant
variable?
How could I fix that without writing my own user-defined type guard?
type PersonDetails = {
gender: 'man' | 'woman',
height: 'tall' | 'short',
}
export type PersonVariant =
| {
gender: 'man',
height: 'tall',
}
| {
gender: 'woman',
height: 'tall',
}
| {
gender: 'woman',
height: 'short',
};
const details = {} as PersonDetails;
function test() {
if (details.gender === 'man' && details.height === 'short') {
return;
}
const variant: PersonVariant = {
gender: details.gender,
height: details.height,
};
}