I want an object of my type to be either of type AB or of type AB & CD. This code looks correct, but gives me no error when I create the object with only 3 keys.

interface AB {
  a: number;
  b: number;
}

interface CD {
  c: number;
  d: number;
}

type ABorABCD = AB | (AB & CD);

// OK
const ab = {
  a: 1,
  b: 2,
}

// OK
const abcd = {
  a: 1,
  b: 2,
  c: 3,
  d: 4,
}
  
// Should not be OK, but it is
const abc: ABorABCD = {
  a: 1,
  b: 2,
  c: 3,
}

// Should not be OK, but it is
const abc: ABorABCD = {
  a: 1,
  b: 2,
  d: 4,
}
0

There are 0 best solutions below