0
interface A {
  common: number
  onlyA: number
}

interface B {
  common: number
  onlyB: number
}

function foo(param: A | B) {
  if(param.common)
}

In this code, TS autocomplete just show me the props that are common to both objects (common), but I want it to show the props that are unique to each object with a sintax like param?.onlyA, because I want it to be OR interface A OR interface B.

If instead of | operator I use & operator, TS understands that there will always be onlyA or onlyB props, autocomplete like param.onlyA.

  • 1
    As param is of type `A` or `B` the autocomplete is correct to only suggest `common`. If you'd parse it like `const parsedParam = param as A` then you'd get both properties auto completed, but then you should have some logic to decide which type it is that you've received. – Max Oct 07 '22 at 11:37

0 Answers0