Skip to content

The extends infer for function generics fails.Β #64073

Description

@vipcxj

πŸ”Ž Search Terms

function generics infer

πŸ•— Version & Regression Information

all version

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=6.0.3#code/PTAEHUFMBsGMHsC2lQBd5oBYoCoE8AHSAZVgCcBLA1UABWgEM8BzM+AVwDsATAGiwoBnUENANQAd0gAjQRVSQAUCEmYKsTKGYUAbpGF4OY0BoadYKdJMoL+gzAzIoz3UNEiPOofEVKVqAHSKymAAmkYI7NCuqGqcANag8ABmIjQUXrFOKBJMggBcISGgoAC0oACCbvCwDKgU8JkY7p7ehCTkVDQS2E6gnPCxGcwmZqDSTgzxxWWVoASMFmgYkAAeRJTInN3ymj4d-jSCeNsMq-wuoPaOltigAKoASgAywhK7SbGQZIIz5VWCFzSeCrZagNYbChbHaxUDcCjJZLfSDbExIAgUdxkUBIursJzCFJtXydajBZJcWD1Ro4zgAHhwAEZwasFDxhJNuI1oHhQABtQSoSicZj8TjsRDSb4AXQAfAAKACU+W8zIA3ooSiVUO1QABhUAAXlVLLZ3A5Hi5nB5-MFwtF-QlUrI0tAAH40GR2CgVckGNBBJAANyarUITiCkwqg3GoXeoOgFT7UAAcjjkBTImEAxoDEEcmYnAY0ncYJ1RFTepToZK4cjsAATNGjTj-YGE0ndSm-QGM1n+oMxPmKIXi6WrOWUCmqzXQE5UPivOLoNAh6AuPEBhIvHnVSGAL7kynUrzJTgNhnMtZm4R24b8HAN00o82OyXfBWK0AarVoiM0VJjTPOk+SZB8GzlJUQ1-Sd9WZY0mWfdkriFYZ3U9b1QF9Ntg1nOsaFgRlo3gjDg0TMBkzTL0+1EHMhwLIsS0sDBYOnRlqxg3U9SfWN2iJVJrxfC0GCtG1QMZcDXQ9dMsNbXtoLDRp6ybfUeNIg8j3ME9aQAZgZJDX05bleQFVCRTFJ0ZQVVAVRwL8f0U-8cRbYCcE-BTtS4kjJ34gzhBwdCZOw+S8KUgiiLglt0w7Ciu3TTNaMHPMGLHZi0C7PV2MUQ8gA

πŸ’» Code

function fn<T1 extends readonly [string, number]>(): T1 {
    type C = T1 extends readonly [string, number] ? true : false;
    const c: C = true; // Type 'true' is not assignable to type 'C'
    const c2: C = false; // Type 'false' is not assignable to type 'C'
    return null as unknown as T1;
}

function fn2<T1 extends string, T2 extends number>() {
    const f = fn<[T1, T2]>();
    type C1 = T1 extends string ? true : false;
    const c1: C1 = true; // Type 'true' is not assignable to type 'C1'
    type C2 = typeof f extends readonly [T1, T2] ? true : false;
    const c2: C2 = true;
}

function fn3<T extends readonly [string, number]>(t: T) {
    const f = fn<T>();
    type C1 = typeof f extends T ? true : false;
    const c1: C1 = true; // Type 'true' is not assignable to type 'C1'
}

πŸ™ Actual behavior

In a generic function, even if I add restrictions to the generic type T, the function seems completely unaware of this, and the extends test will never pass.

πŸ™‚ Expected behavior

C in fun should be true
C1 in fun2 should be true
C1 in fun3 should be true

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