Skip to content

Compiler allows violation of a generic function type interface #11877

Closed
@albrow

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.

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