microsoft / microsoft/TypeScript

Incorrect error on legitimate type overlap

Open
#41,402 8 comments 0 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: 4.1.0-dev-20201102

Search Terms:

This condition will always return 'false' since the types 'X' and 'Y' have no overlap.

Code

function fn<T>(t: T) {
    return t === '' // error
}

fn('') // but here we pass it

https://www.typescriptlang.org/play?jsx=0#code/GYVwdgxgLglg9mABMMAeAKgPgBRQFyLoCUiA3gFCJWIBOAplCDUlIgLweIDkXiA9H0R0aNODXIBfcuRTYeJAYgDuYgNYBnIA

Expected behavior:

It works when we extend unknown. While this works on simple generics, it becomes inconvenient when the type parameter is nested deeper in a type structure.

function fn<T extends unknown>(t: T) {
    return t === '' // works
}

fn('') // works

https://www.typescriptlang.org/play?jsx=0#code/GYVwdgxgLglg9mABMMAeAKogpgDylsAEwGdFwBrMOAdzAD4AKKALkXQEpEBvAKEX8QAnLFBCCkURAF4ZiAORzEAeiWJqcQeWI8Avjx4oGCzirUatQA

Actual behavior:

This condition will always return 'false' since the types 'T' and 'string' have no overlap.

Related issues
https://github.com/microsoft/TypeScript/issues/27910

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

Reproduce the generic comparison in the TypeScript compiler using the provided code and Playground links, then compare it with the T extends unknown example. Investigate the type-overlap diagnostic and related issue #27910; done means the legitimate comparison no longer reports that it will always return false while invalid comparisons remain diagnosed.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.