Skip to content

Type inference not working for this particular use of Generics? #8952

Closed
@coreh

Description

@coreh

Hey there. I was experimenting with generics, and noticed that type inference didn't work as I expected for this particular case. I'm not entirely sure if this is an actual blind spot of the type inference mechanism, or if my declaration doesn't mean what I think it means. it's a bit similar to the “Curiously recurring template pattern” described on #5949, but isn't actually the CRTP, since there's no actual recursion involved. Thanks in advance for looking into this issue.

TypeScript Version:

1.8.10

Code

interface IFoo {
    foo: 'foo'; // Could be anything
}

interface IWrapped<I> {
    getObject(): I;
}

function unwrap<I, T extends IWrapped<I>>(x: T): I {
    return x.getObject();
}

let x: IWrapped<IFoo>/* = ... */;
let y = unwrap<IFoo, IWrapped<IFoo>>(x); // Type is IFoo (correct)
let z = unwrap(x); // Type is {} (incorrect?)

Expected behavior:

I expected the compiler to infer the type of z as being IFoo, like when using explicit generic parameters on y.

Actual behavior:

The compiler inferred the type of z as being {}.

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions