0
type xyz = { a:string,b:string }
type pqr = {c:string, d:string}
type test = xyz | pqr

const abc:test = {
    a:"sdf",
    b:"sdf",
    c:"fd"
}

if("c" in abc){
    console.log(abc.c)
    console.log(abc.d)
}

in the above example, typescript doesn't throw any error, there is no key d in the object abc, any insights on this behavior of typescript in my opinion either it should not allow the partial or it should have checks to have key for both narrowing on basis of one key should be not allowed

Playground Link

  • 1
    Very similar to https://stackoverflow.com/questions/52677576/typescript-discriminated-union-allows-invalid-state Same type should help. TS will still allow any keys of the union in object literals. – Titian Cernicova-Dragomir Jan 28 '22 at 13:24
  • 1
    @T.J.Crowder Ok . Kes, if you feel that does not answer your question, just adda comment and we'll sort it out – Titian Cernicova-Dragomir Jan 28 '22 at 13:27
  • 1
    @kes - We can reopen the question if it turns out to be different, but it looks the same. And yeah, this definitely is one of the odd things about TypeScript. :-) – T.J. Crowder Jan 28 '22 at 13:28
  • my doubt was, will this behavior will not make typescript less reliable in, it should not have this conflicting type of behaviour – kes.mahawar Jan 28 '22 at 13:48

0 Answers0