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

Make functions generic when contextually typed by a generic signature #5737

Closed
zpdDG4gta8XKpMCd opened this issue Nov 20, 2015 · 2 comments
Closed
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@zpdDG4gta8XKpMCd
Copy link

type Foo = (one: string) => number;

function fooOver() : Foo {
    return one => parseInt(one); // <-- one is string
}

type Bar = <a>(one: string) => a;

function barOver() : Bar {
    return one => parseInt(one); // <-- one is god knows what
}
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Nov 20, 2015
@RyanCavanaugh
Copy link
Member

Technically this is according to per spec (I know, your favorite response), but I think we should do better here.

Section 4.2.3 (https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#423-contextually-typed-expressions) says

In the body of a function expression or arrow function that has no return type annotation, if the function expression or arrow function is contextually typed by a function type with exactly one call signature, and if that call signature is non-generic, return expressions are contextually typed by the return type of that call signature.

We already said that we should contextually type parameters when type annotations are present somewhere --- no reason we shouldn't contextually type using the non-generic parameter positions or non-generic return positions.

@RyanCavanaugh RyanCavanaugh added the Spec Issues related to the TypeScript language specification label Nov 20, 2015
ahejlsberg added a commit that referenced this issue Dec 3, 2015
Improved union/intersection type inference
Conflicts:
	src/compiler/checker.ts
@ahejlsberg
Copy link
Member

@RyanCavanaugh There is no easy way to tell whether a particular type references a type parameter and I honestly think that we either contextually type all parameters or none. Anything in between just gets even more confusing.

@mhegazy mhegazy added Suggestion An idea for TypeScript In Discussion Not yet reached consensus and removed Bug A bug in TypeScript Spec Issues related to the TypeScript language specification labels Dec 8, 2015
@DanielRosenwasser DanielRosenwasser changed the title another inferrence strangeness Make functions generic when contextually typed by a generic signature Aug 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants