0

I would have thought that ObjectOne | ObjectTwo meant ObjectOne or ObjectTwo, not the combination of ObjectOne and ObjectTwo which is what it seems from below:

type A = { a: number }
type B = { b: number }
type AB = A | B
type ABs = AB[]
// valid as expected
const validList: ABs = [{ a: 1 }, { b: 2 }]
// valid, but should be invalid due to {a: 1, b: 2}
const shouldBeInvalidList: ABs = [{ a: 1 }, { b: 2 }, { a: 1, b: 2 }]

What am I missing here, and how can I gain this object or that object with typescript?

The use case here is to have an array, that either accepts one specific object, or another specific object, but no extra properties, and no combinations.

balupton
  • 47,113
  • 32
  • 131
  • 182

0 Answers0