Skip to content

Method infers the wrong type when passing a value that is typealiased that uses union and intersection types #55648

Closed
@NatanLifshitz

Description

@NatanLifshitz

🔎 Search Terms

typealias inference, union type inference, intersection type inference, inconsistent inference

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about type inference

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.0-dev.20230906#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgISmEwGs0BJFNLXOAbwCgBfBh0SWOdAVyWxgQQkcAEZQIZJAFUAzpgDmwADwBlADRwAogD4AFERncANjABccKUkFWAXmlUadASnrNW7aPBgBPMHkvWCHZQDlracAC8cLoqcAA+Wi4AZAREpBRUGDjA7uCecD5+FlZCQWiyCnhRAaXBSgBuEAgAJhoyMFDI8tqsPHwCQlyYCEbAzZToaMB8wLoujHCLcNhC7XAg5jW25XKKkXCKMJTtmDNzDEtwAPRXcADCABbA2CRDIzJc0HBIEHBpMkILksxBJphVFLoQE43Aw+vxrHBDNhcDIZBMpmd5kDFiskGsNsVAnVGi02h0uuEoodjjBTrhzpcbnAVJgALZ4QrAD6YD4iYDoaB4ETceDYJ4vRHcZHAMYybGicSScGzKEw2G8eGDal42mYzYlbZQZX0JhAA

💻 Code

export interface BreakerInterface {
}

export function brokenUsage<S, E>(result: Unionizer<S, E>) {
}

export type Unionizer<S, E> = (S | E) & BreakerInterface

export type UnionizerUsage = Unionizer<void, string>

function failedInference() {
    const x: UnionizerUsage = getInstance()
    // Check fails for no reason
    brokenUsage(x)
}

function successInference() {
    const x: Unionizer<void, string> = getInstance()
    // Same types as before but check succeeds
    brokenUsage(x)
}


function getInstance(): UnionizerUsage {}

🙁 Actual behavior

Code fails to compile due to "Type 'void & BreakerInterface' is not assignable to type 'string'" which is actually due to the brokenUsage call inferring x as Unionizer<string, string> instead of Unionizer<void, string>.

🙂 Expected behavior

UnionizerUsage is expanded to Unionizer<void, string> and code compiles

Additional information about the issue

No response

Metadata

Metadata

Assignees

Labels

Design LimitationConstraints of the existing architecture prevent this from being fixed

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions