question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect error on legitimate type overlap

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
millspcommented, Nov 5, 2020

Just of course the type information string is lost and t is T instead of T & string

That’s exactly what I’m complaining about.

The same would apply without the additional type-guard

I get that a generic T intersected with any or unknown yields T.

I’m pointing out the fact that we could safely narrow T with the type that sits on the rhs of the comparison operator.

But @RyanCavanaugh seems to say that it’s not possible, and I would like to know why should we have such a limitation.

Another example:

function fn<A, B>(a: A, b: B) {
    if (a === b) { // fails here
		// ...
    } else {	
		// ...
    }
}
const equals = <A>(thing: unknown, other: A): thing is A => {
    return thing === other;
};

function fn<A, B>(a: A, b: B) {
    if (equals(a, b)) { // works
        // a: A & B
    } else {
        // a: A
    }
}
1reaction
RyanCavanaughcommented, Nov 5, 2020

A generic type parameter is comparable to the type of its constraint if a constraint was explicitly declared

Read more comments on GitHub >

github_iconTop Results From Across the Web

This condition will always return 'false' since the types have no ...
TypeScript Version: 3.0.0-dev.20180712 Error: This condition will always return 'false' since the types 'A' and 'I' have no overlap.
Read more >
Fake Pivot overlap errors, How to fix them/tell the difference
Now I can see the real size of the Pivot table and either these are actually so large (due to data errors) that...
Read more >
Type I & Type II Errors | Differences, Examples, Visualizations
In statistics, a Type I error is a false positive conclusion, while a Type II error is a false negative conclusion.
Read more >
This condition will always return 'false' since the types '1' and ...
This condition will always return 'false' since the types '1' and '2' have no overlap. what am i doing wrong? .ts status: number;...
Read more >
Type I and type II errors - Wikipedia
In statistical hypothesis testing, a type I error is the mistaken rejection of an actually true null hypothesis while a type II error...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found