Skip to content

Compiler allows violation of a generic function type interface #11877

Closed

Description

@albrow

TypeScript Version: 2.0.6

Code

interface GenericIdentityFn {
    <T>(arg: T): T;
}

function identity(n: number): string {
    return n.toString();
}

let myIdentity: GenericIdentityFn = identity;

Expected behavior:

The code should not compile because identity does not satisfy the constraints of GenericIdentityFn. Namely, it accepts an argument of type number and returns type string, whereas the interface GenericIdentityFn describes a function that accepts an argument of type T and returns type T.

Actual behavior:

The code compiles.

Other notes:

This is based on an example from https://www.typescriptlang.org/docs/handbook/generics.html.

Activity

  1. HerringtonDarkholme commented on Oct 27, 2016

    @HerringtonDarkholme
    Contributor

    Related #11537

  2. mhegazy commented on May 18, 2017

    @mhegazy
    Contributor

    Tracked by #5616

  3. 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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions