Skip to content

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

Description

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

Activity

  1. added
    Suggestion
    In Discussion
    By Design
    and removed
    In Discussion
    Suggestion
    By Design
    on Nov 11, 2015
  2. RyanCavanaugh commented on Nov 11, 2015

    @RyanCavanaugh
    Member

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

  3. added
    Fixed
    on Jun 8, 2017
  4. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedIn DiscussionSuggestion

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions