Skip to content

Type inference of generics is order-dependent #6627

Closed
@sandersn

Description

@sandersn
declare var f: { 
    <U>(x: U): U,
};
declare function pipe<T>(arg: T, dit: (arg: T) => T): T; // welkom in F#!
declare function apply<V>(dit: (arg: V) => V, arg: V): V;
let n = pipe(12, f);
let m = apply(f, 12);

n: number as desired but m: {}. It looks like, during the second pass of type parameter inference for apply, the call signature is inferred from dit's argument before it is inferred from arg. Since f is itself generic, the default is {}. @ahejlsberg, is this intended behaviour? It seems like a bug to me.

Also I am not sure why getSingleCallSignature special-cases single-call-signature-only objects. You get yet a different result if you have an extra property on f:

declare var f: { 
    <U>(x: U): U,
    hunch(x: number): number
};
...
let n = pipe(12, f)
let m = apply(f, 12)

Now both n: any and m: any because getSingleCallSignature doesn't return a signature, so instantiateTypeWithSingleGenericCallSignature doesn't do its instantiation at all.

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