Skip to content

Simple Comparison of Generics not allowed? #38220

@bradennapier

Description

@bradennapier

TypeScript Version: 3.8

Search Terms:
generic comparison
compare generic strings

Code

function test<A, B>(one: A, two: B): A {
  if (one === two) {
    throw new Error('no');
  }
  return one;
}

const obj = {};

test(obj, obj);

Expected behavior:

While the generics are generally indicating that one and two are independent values, they can still potentially be the same underlying value so they should be able to at least be checked for equality without casting them first, especially since there is no error produced when calling the function with identical values in the first place.

At the very least the error message should be more specific since the assumption it is providing is not correct in since the condition returns true :-P

( This is clearly a simplified version of where I ran into this since there'd be no reason to use generics in this specific case :-) )

Actual behavior:

Shown in example

Playground Link: Playground

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions