Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type inference fails on instance method call that returns same type #35758

Closed
joshuakb2 opened this issue Dec 18, 2019 · 1 comment
Closed

Type inference fails on instance method call that returns same type #35758

joshuakb2 opened this issue Dec 18, 2019 · 1 comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@joshuakb2
Copy link

joshuakb2 commented Dec 18, 2019

Search Terms: infer, inferred, declaration, generic function

Code

// Things are objects that store a function
class Thing<In, Out> {
    f: (x: In) => Out;

    private constructor(f: (x: In) => Out) {
        this.f = f;
    }

    // Creates an identical thing
    public another(): Thing<In, Out> {
        return new Thing<In, Out>(this.f);
    }

    // Creates a thing with the given function
    public static create<In, Out>(f: (x: In) => Out): Thing<In, Out> {
        return new Thing<In, Out>(f);
    }
}

// Type inference works here, understands that x => x is string => string
const test: Thing<string, string> = Thing.create(x => x);

// Type inference does not work here, even though .another() returns something with the same type as the original thing
const test2: Thing<string, string> = Thing.create(x => x).another();

// Type inference works here because we helped it along by defining x: string, but it's redundant.
const test3: Thing<string, string> = Thing.create((x: string) => x).another();

Expected behavior: test, test2, and test3 would all be inferred properly.

Actual behavior: test2 produces an error: "Type 'unknown' is not assignable to type 'string'."

Playground Link: Playground Link

Related Issues: (I couldn't find any)

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Jan 7, 2020
@RyanCavanaugh
Copy link
Member

The contextual type of an expression can supply inference candidates via the return type, but this process is not recursive and it's very tenuous to apply that logic any further than it already goes.

#30134 would be a prerequisite to handling this in all cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants