Trying to establish a Consume
type with two different payloads.
interface Eat {
category: 'meat' | 'vegetable'
}
interface Drink {
size: string
}
interface Consume {
type: 'eat' | 'drink'
payload: Eat | Drink
}
Encountering issues with if statements
function doSomething(c: Consume): string {
if (c.type === 'Eat') {
const e: Eat = c.payload <----- ERRORS
return e.category
}
return ''
}
Errors triggered:
Type Eat | Drink is not assignable to type 'Drink'. Property 'size' is missing in type 'Eat' but required in type 'Drink'. ts(2322)
Questioning the feasibility of having two distinct schemas within a TypeScript interface Consume
.