Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wired behavior with conditional checking on multiple conditions. #46902

Open
zheeeng opened this issue Nov 23, 2021 · 1 comment
Open

Wired behavior with conditional checking on multiple conditions. #46902

zheeeng opened this issue Nov 23, 2021 · 1 comment
Labels
Bug A bug in TypeScript
Milestone

Comments

@zheeeng
Copy link

zheeeng commented Nov 23, 2021

Bug Report

πŸ”Ž Search Terms

conditional type on tuple

πŸ•— Version & Regression Information

v4.5.2

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type AA<T extends string> = T
type BB<T extends number> = T
type CC<T extends boolean> = T

// x
// Type 'A' does not satisfy the constraint 'string'.(2344)
// Type 'B' does not satisfy the constraint 'number'.(2344)
// Type 'C' does not satisfy the constraint 'boolean'.(2344)
type Test<A, B, C> = 
    [A, B, C] extends [string, number, boolean]
        ? AA<A> | BB<B> | CC<C>
        : never
// ??: type Result = string | number | boolean
type Result = Test<string, number, boolean>

// o
type Test2<A, B, C> = 
    [A] extends [string]
        ? [B] extends [number]
            ? [C] extends [boolean]
                ? AA<A> | BB<B> | CC<C>
                : never
            : never
        : never

// o
type Test3<A, B, C> = 
    A extends string
        ? B extends number
            ? C extends boolean
                ? AA<A> | BB<B> | CC<C>
                : never
            : never
        : never

πŸ™ Actual behavior

  1. In Test1 A, B and C are incompatible with type constraints. As a comparison, Test2 is passed.
  2. We still can get the Result type even the Test1 doesn't pass the validation by the type checker

πŸ™‚ Expected behavior

  1. Don't sure about Test1 or Test2 which is valid.
  2. Don't sure should we get the expected Result type when the advanced type doesn't pass the checker.
@RyanCavanaugh
Copy link
Member

The error that we couldn't deduce that the type parameter met the constraint occurs and the type declaration is otherwise unaffected; you can see this in code like

type Foo<T extends 1> = T;
// M = 2
type M = Foo<2>;

What "should" happen here is effectively undefined behavior since the program contains a contradiction and thus any result will be "wrong" by some definition.

I'm not sure what causes [A] extends [T] to cause a narrowing of A -> T but not in the 3-tuple case. Seems fixable in principle

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Nov 23, 2021
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Nov 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants