Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simple Comparison of Generics not allowed? #38220

Open
bradennapier opened this issue Apr 27, 2020 · 1 comment
Open

Simple Comparison of Generics not allowed? #38220

bradennapier opened this issue Apr 27, 2020 · 1 comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Milestone

Comments

@bradennapier
Copy link

bradennapier commented Apr 27, 2020

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

@RyanCavanaugh RyanCavanaugh added Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript labels Apr 29, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Apr 29, 2020
@Rudxain
Copy link

Rudxain commented Jun 20, 2024

I'm having the "opposite" issue: #58938 . A generic can be compared with itself, even if its concrete type isn't comparable (such as symbols)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Experience Enhancement Noncontroversial enhancements Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants