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

Contextual typing does not flow backward through function return types to the function arguments #5256

Closed
drarmstr opened this issue Oct 14, 2015 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@drarmstr
Copy link

Consider the following case:

class Entry<T> {
    constructor(callback: (v: T) => number) { }
}
interface Widget {
    list: Entry<{ foo: number }>[];
}

var widget: Widget = {
    list: [
        new Entry((v) => v.foo), // Error accessing v.foo
    ]
};

We get a compiler error in the callback accessing v.foo that foo does not exist on type {} since {} is the default type when it is unable to infer the type. However, in this case we know from the Widget definition that T must be typed to {foo:number}. But, it appears TypeScript is not using this as an inference-site to properly determine T.

@RyanCavanaugh
Copy link
Member

More simply:

class Entry<T> {
    constructor(callback: (v: T) => number) { }
}
var x: Entry<{foo: number}> = new Entry(v => v.foo);

@RyanCavanaugh RyanCavanaugh changed the title Type inference is not using a type declaration annotation as an inference-site. Contextual typing does not flow backward through function return types to the function arguments Oct 14, 2015
@mhegazy mhegazy added the Question An issue which isn't directly actionable in code label Oct 14, 2015
@RyanCavanaugh
Copy link
Member

Let's go even simpler:

function foo<T>(x: (n: T) => T): T { return x(undefined); }
var x: string = foo(p => p.substr(2));

(placeholder since I need to write a longer explanation)

@drarmstr
Copy link
Author

@mhegazy, Sorry, what is the implication of the Question tag? Is there a follow-up question for me?

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Question An issue which isn't directly actionable in code labels Oct 21, 2015
@RyanCavanaugh
Copy link
Member

Sorry, I forgot to write the follow-up I alluded to.

After walking through this with the spec, I think the rule we would need to support this is that a function call (or constructor call) contextually typed by T creates an inference site corresponding to the return type of that function or constructor.

That said, it's not clear what runtime semantics this rule corresponds to, or that it wouldn't be an undesirable breaking change.

@johnnyreilly
Copy link

It would be fantastic if this could be looked into.

@RyanCavanaugh
Copy link
Member

Moving discussion to #11152 which has clearer explanation

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Oct 31, 2016
@RyanCavanaugh
Copy link
Member

I believe this is fixed by #16072

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants