microsoft / microsoft/TypeScript

Simple Comparison of Generics not allowed?

Open
#38,220 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experience Enhancement Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the TypeScript 3.8 Playground link and the generic test<A, B> reproduction in the issue. Investigate the type-checker diagnostic for comparing one === two when A and B are independent type parameters. Done means the comparison behavior or its diagnostic matches the stated expectation, with coverage for the shown example.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.