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 for generic functions #11537

Closed
sergey-shandar opened this issue Oct 11, 2016 · 9 comments
Closed

Type inference for generic functions #11537

sergey-shandar opened this issue Oct 11, 2016 · 9 comments
Labels
Fixed A PR has been merged for this issue

Comments

@sergey-shandar
Copy link
Contributor

TypeScript Version: 2.0.3

Code

function id<T>(v: T): T { return v; }

function applyToNumber<T>(v: T, toNumber: (x: T) => number): number {
    return toNumber(v);
}

applyToNumber(5, v => v); // ok
applyToNumber(5, id); // ok
applyToNumber("aaa", v => v); // error, as expected.
applyToNumber("aaa", id); // ok, but an error is expected.

Expected behavior:
error

Actual behavior:
no error.

@ghost
Copy link

ghost commented Oct 11, 2016

Related: #11311

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented Oct 11, 2016

might be dup a new shiny case of #3038

@sergey-shandar
Copy link
Contributor Author

If it's compatibility problem, it would be really helpful to have a "strictTyping" compilation mode which would prohibit converting the any type to other types without explicit cast.

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented Oct 11, 2016

they say without any it will be an infinite recursion which i don't get because an explicit lambda (v => v in your case) doesn't have this problem, if so.. why would not TS handle all function references as explicit lambdas behind the scenes to avoid this recursion problem

@sergey-shandar
Copy link
Contributor Author

sergey-shandar commented Oct 11, 2016

An ugly workaround

function getId<T>(): (v: T) => T { return v => v; }

applyToNumber(5, getId()); // error, can't infer the 'T' type.
applyToNumber(5, getId<number>()); // ok but 'v => v' looks better.

@mhegazy
Copy link
Contributor

mhegazy commented May 18, 2017

related to #5616, and possibly #417, #5487, #9107, #9659, #9660,
#10247, #11311, #11343, #5616

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented May 18, 2017

looks like its time to bite that bullet or else you gonna break that all time record of the number of open issues related to the same problemo

@AllainPL
Copy link

The sample given:

applyToNumber("aaa", id); // ok, but an error is expected.

in TS 2.6 returns error:

Argument of type '<T>(v: T) => T' is not assignable to parameter of type '(x: string) => number'.
  Type 'string' is not assignable to type 'number'.

So I assume it's fixed.

@mhegazy mhegazy added Fixed A PR has been merged for this issue and removed Needs Investigation This issue needs a team member to investigate its status. labels Nov 13, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 13, 2017

thanks! closing.

@mhegazy mhegazy closed this as completed Nov 13, 2017
@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
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants