-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Description
Complete the following code using discriminated unions, so that TS gives no errors:
interface Base {
foo: string;
bar: string;
}
interface A extends Base {
bazA: string;
}
interface B extends Base {
bazB: string;
}
type AOrB = A | B;
const aOrB: AOrB = {
foo: 'foo',
bar: 'bar',
...(Math.random() > 0.5 ? { bazA: 'bazA' } : { bazB: 'bazB' }),
}
if (/* condition for a */) {
console.log('aOrB is an A', aOrB.bazA);
} else {
console.log('aOrB is a B', aOrB.bazB);
}
process.exit(0);You need to add a mechanism to implement coditions.
What to google
- discriminated unions (also called discriminated types or tagged types)
Metadata
Metadata
Assignees
Labels
No labels
Type
Projects
Status
✅ Done