Skip to content

Type inference failure on generic type with multiple optional properties #1801

Closed
@Nemo157

Description

@Nemo157
interface Client {
    wrap<T>(interceptor: Interceptor<T>, config?: T): Client;
    typedWrap(interceptor: Interceptor<Config>, config?: Config): Client;
}

interface Interceptor<T> {
    parent(client?: Client, config?: T): Client;
}

interface Config {
    mime?: string;
    accept?: string;
}

var client: Client;
var config: Config = { mime: 'application/json' };
var mime: Interceptor<Config>;

client.wrap(mime, config);
client.wrap(mime, { mime: 'application/json' });

client.typedWrap(mime, config);
client.typedWrap(mime, { mime: 'application/json' });

with tsc version 1.4.1.0 gives

test.ts(20,1): error TS2453: The type argument for type parameter 'T' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
  Type argument candidate 'Config' is not a valid type argument because it is not a supertype of candidate '{ mime: string; }'.

I can't tell exactly what's going wrong here, but it appears to be some issue at the intersection between generics and plain objects implementing interfaces. I would assume by the fact that both assigning the object to a typed variable and using a specified type in the function that Config is in fact a supertype of the candidate; this fact is just not being picked up during the type inference stage.

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