Skip to content

Inconsistent behavior between intersection literal type during inferenceΒ #57776

Closed as not planned
@unional

Description

@unional

πŸ”Ž Search Terms

literal intersection infer extract value

πŸ•— Version & Regression Information

Behavior changed since 5.1

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240314#code/C4TwDgpgBASgjFAvFAdgVwLYCMICcoQAewEKAJgM6qY74D8Uwua0AXFAGYCGANhdAHoBjZhABQoSLABMSKAiIlyVBAyYso7bn0HD14ydBgBmOQuKlK1bHihrRmzr35QhIlhPBGALHPQ38ADIoAG8oLnYEAF8CC2VrWjt3NicdVz1RTykYAFY-GltgsIj5KBjFSyp-ROCASxQOWwBVJJb2FAgAN1s3YsiyrKMANjMoIvD+8rirBDqG5tbHDu78XonSqMHYAHZR8ZLo2KUrasKoesb8FoY21C6e4VnQ9eigA

πŸ’» Code

type R1 = number extends number ? true : false // true
type R2 = 1 extends 1 ? true : false // true
type R3 = 1 extends number ? true : false // true
type R4 = number & { a: 1 } extends number ? true : false // true
type R5 = number & { a: 1 } extends number & infer U ? U : never // { a: 1 }
type R6 = 1 & { a: 1 } extends 1 & infer U ? U : never // { a: 1 }
type R7 = 1 & { a: 1 } extends number & infer U ? U : never // 1 & { a: 1 }

πŸ™ Actual behavior

see above

πŸ™‚ Expected behavior

R7 should returns { a: 1 } as 1 extends number.

Due to this and #54648 (comment),
currently it doesn't seem to have a way to write the IsNegative type (among others) anymore.

This is one of the problems caused by #54648. It would be great if that can be re-evaluated.

Additional information about the issue

tbh, IMO many types I wrote in type-plus would be best provided by TS itself to make the type system more predictable and enable us to write better types. One suggestion is to support typeof T for types, but that's a different topic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions