Skip to content

Double (exclusion) inference doesn't narrow union objectΒ #61075

@exoRift

Description

@exoRift

πŸ”Ž Search Terms

"double inference", "exclusion inference", "union object narrow"

πŸ•— Version & Regression Information

I observed this issue on versions

  • 5.4.5
  • 5.7.3

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.3#code/C4TwDgpgBAKhDOwoF4oAoDeBYAUFfUAxgBYSEDWAXFAOQBOCA9gDYBuEAJjbgVMMQEsAdgHNqQgK4BbAEYQ6uAL5QAPlGx4CJMlVrNGAQw7CRNVbXl1GDLjwL8T1CUI4QAZsM5KAlLlyFGIUQoN0ZGajhg1AxlA3hYBGA-HAE3dFDGADptChRkVBp9IxMzFTUM7NJc-ILLa04ab3U7fAqc8haoAHouqAA9AH5OiodRTp7+oZxlCGZ4aA1eNqqOzXwJweGwzNGRcd7N6dwgA

πŸ’» Code

type Test = ({
    check: 'resolved'
    thing: number
} | {
    check: 'loading' | 'errored'
    thing: undefined
})

const foo: Test = {} as Test

if (foo.check === 'loading' || foo.check === 'errored') {
    foo.check
    // ^?
    foo.thing
    // ^?
} else {
    foo.check
    // ^?
    foo.thing
    // ^?

  // `foo.check` is `'resolved'` here, but `foo.thing` is still `number | undefined`
}

πŸ™ Actual behavior

foo.check` is `'resolved'` here, but `foo.thing` is still `number | undefined

This is impossible considering the type

type Test = ({
    check: 'resolved'
    thing: number
} | {
    check: 'loading' | 'errored'
    thing: undefined
})

πŸ™‚ Expected behavior

I expected foo.thing to be of type number since foo.check is of type 'resolved'

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions