Skip to content

The ?: inference failed; no selection was made between ifTrue and ifFalse. #58245

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

Closed
LiST-GIT opened this issue Apr 19, 2024 · 1 comment
Closed

Comments

@LiST-GIT
Copy link

πŸ”Ž Search Terms

?:

πŸ•— Version & Regression Information

v5.4.5

⏯ Playground Link

https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true#code/MYGwhgzhAECC0G8BQ1XQgFzBglsaAogG4CmAdhgCoCeADiRAFyIppsDkJpFAcmALYl2zANoBGADTQATAF0JrVAF8A3EkXQAJiVBgATiWgj2sWO1nMMdEgHsAZnDUaMDDAB4A0tBIAPF2U0YWCkAMW8-ckDoD2NTc2NiciprCHMpSnD-KIBrEmp7aBCAPgAKDTYMAAscJmiFNgrrZkp6htQwZhCRSllMyJgwMmoRXoB+aDIAV34AIxI9aGZMPRwyAHNoAHpN6EpKw1W7efJgQ32DaAB3SAmbDHRJ4FOoO0mQFWgqwyt6dEqbN6aaBzLQkFx6firMAzECGG4kHBfBZgCbTOYLGzI9AYFbrAB0GgAlJ1ur0ENADBhJnoyKiQCBoDdBtQPkp1GwIJEACLYMAlQksNqoIj6RnQAC8n2qEDxLkwJU43AwfEE7Ck7Gk7EJH220AAyjjVhsard7mAniRaFgYSQCUKxZLxFI5Fsdl0etATRBHs8IK96dRPWQjnoDJo7UKRQsZhKpTVZa4FVwkiqhM7ta7oDw0fNPTAyHdGRardDYRG2jHHdIpABmXq6929L0+hh+t4gQOHeZh8vQNlKIA

πŸ’» Code

class A {
    static EventTypes: {
        'eventName': [1, 2],
    };

    declare ['AA']: typeof A;

    test<K extends A, F extends K['AA']['EventTypes'], T extends keyof F>(
        this: K,
        type: T,
        a: F[T] extends any[] ? number : string // The inference here was not successful; the type should be determinable as either a number or a string.
    ): F[T] { return null as any; }

    sendData() {
        var a = this.test('eventName', '2'); // String is not acceptable.
        a = [1, 2] // F[T] is successfully inferred.
        var b = this.test('eventName', 2); // Number is not acceptable.
        b = [2, 3] // F[T] is successfully inferred.
    }
}

πŸ™ Actual behavior

Argument of type 'string' is not assignable to parameter of type 'this["AA"]["EventTypes"]["TCP:SEND_DATA"] extends any[] ? number : string'.
Argument of type 'number' is not assignable to parameter of type 'this["AA"]["EventTypes"]["TCP:SEND_DATA"] extends any[] ? number : string'.

Using the ?: operator, neither ifTrue nor ifFalse was selected. I am unsure of what type to pass in to satisfy the conditions.

πŸ™‚ Expected behavior

I believe the ?: operator should never fail; it should always select and return one of the options.
Of course, in my test, I hope it returns a number.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

I believe the ?: operator should never fail; it should always select and return one of the options.

It's not failing, your type simply can't be resolved because the type of this is not known. It could be very well a subclass with a more constrained property. Such is the limitation with conditional types. When dealing with conditional types all involved types must be known, and what type this actually is is not known.

See also #51288 and other issues regarding this and conditional types.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants