Skip to content

Unable to infer types correctly from generic on function #26418

Closed
@agubler

Description

@agubler

TypeScript Version: 3.0.1-insiders.20180726 (typescript playground version)

Search Terms:

generic, inference

Code

Note: noImplicitAny set to true

class BaseClass<P = any> {
    public props: P;
}

function w<W extends BaseClass>(
    ctr: (new () => W),
    props: W['props']
) { }

interface MyClassProps {
    myFunc(options: { foo: string }): void;
}

class MyClass extends BaseClass<MyClassProps> {}

// eplicit passing the generic
w<MyClass>(MyClass, {
    myFunc(options) {
        options.foo; // correctly infers prop
        options.bar; // correctly errors
    }
});

// inferring `myFunc` sig types based on `MyClass` argument
w(MyClass, {
    myFunc(options) { // doesn't infer argument type
        options.foo;
        options.bar;
    }
 });

Expected behavior:

Expected the argument of the function to be inferred correctly.

Actual behavior:

Doesn't infer the argument type or provide auto complete on the props of the w() function (does however type check)

Playground Link: http://www.typescriptlang.org/play/#src=class%20BaseClass%3CP%20%3D%20any%3E%20%7B%0A%20%20%20%20public%20props%3A%20P%3B%0A%7D%0A%0Afunction%20w%3CW%20extends%20BaseClass%3E(%0A%20%20%20%20ctr%3A%20(new%20()%20%3D%3E%20W)%2C%0A%20%20%20%20props%3A%20W%5B'props'%5D%0A)%20%7B%20%7D%0A%0Ainterface%20MyClassProps%20%7B%0A%20%20%20%20myFunc(options%3A%20%7B%20foo%3A%20string%20%7D)%3A%20void%3B%0A%7D%0A%0Aclass%20MyClass%20extends%20BaseClass%3CMyClassProps%3E%20%7B%7D%0A%0A%2F%2F%20eplicit%20passing%20the%20generic%0Aw%3CMyClass%3E(MyClass%2C%20%7B%0A%20%20%20%20myFunc(options)%20%7B%0A%20%20%20%20%20%20%20%20options.foo%3B%20%2F%2F%20correctly%20infers%20prop%0A%20%20%20%20%20%20%20%20options.bar%3B%20%2F%2F%20correctly%20errors%0A%20%20%20%20%7D%0A%7D)%3B%0A%0A%2F%2F%20inferring%20%60myFunc%60%20sig%20types%20based%20on%20%60MyClass%60%20argument%0Aw(MyClass%2C%20%7B%0A%20%20%20%20myFunc(options)%20%7B%20%2F%2F%20doesn't%20infer%20argument%20type%0A%20%20%20%20%20%20%20%20options.foo%3B%0A%20%20%20%20%20%20%20%20options.bar%3B%0A%20%20%20%20%7D%0A%20%7D)%3B

Related Issues: Possibly related to #14829?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions