Skip to content
New issue

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

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

Already on GitHub? # to your account

Compiles without error when the return type signature of variable doesn't match that of the generic function that it is being assigned to #5616

Closed
prateekbhatt opened this issue Nov 11, 2015 · 1 comment
Labels
Fixed A PR has been merged for this issue In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@prateekbhatt
Copy link

Compiling the code below:

function identity<T>(arg: T): T {
    return arg;
}

var myIdentity: <T>(arg: T) => string = identity;

var a = myIdentity<string>("string")
var b = myIdentity<number>(100)

outputs the following javascript:

function identity(arg) {
    return arg;
}
var myIdentity = identity;
var a = myIdentity("string");
var b = myIdentity(100);

I put the type signature of the variable myIdentity as <T>(arg: T) => string
where the return type is string instead of T (that I expect to be correct).

I expected the Typescript compiler to show an error because the return types
don't match. However, the code compiled without any error.

Specs:

Mac OS X 10.11

tsc --version outputs:
message TS6029: Version 1.6.2

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus By Design Deprecated - use "Working as Intended" or "Design Limitation" instead and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript By Design Deprecated - use "Working as Intended" or "Design Limitation" instead labels Nov 11, 2015
@RyanCavanaugh
Copy link
Member

Technically a duplicate of #3410, but I want to discuss this again so we can understand why we don't do this.

@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Jun 8, 2017
@mhegazy mhegazy closed this as completed Jun 12, 2017
@mhegazy mhegazy added this to the TypeScript 2.4.1 milestone Jun 12, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Fixed A PR has been merged for this issue In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants