Skip to content

Generics in functions #974

Closed
Closed
@paztis

Description

@paztis

I used to use generics in Java to define constraints between the different arguments of a function.
In TypeScript, they are a little more limited.
For example:

function test(a: T, b: T): void {
}
test(1, 2); //It works with T as number
test("a", 2); //It works with T as {}
test("a", 2); //Error "a" is not a number

I wish I did not have to specify the "number" type as we can do in Java.
For me test("a", 2) should cause an error. Compiler inference should not find {} as common type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions