microsoft / microsoft/TypeScript

Predicates break arbitrarily when intersected with some types. (Probably when the predicate is generic).

Open
#60,048 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Intersection Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔎 Search Terms

predicate intersection

🕗 Version & Regression Information

latest version v5.6.2

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.2#code/JYOwLgpgTgZghgYwgAgCpQK5gBYE8AKUEAJsAnJMgN4CwAUMo8gDyoB8AFAPYBcaAlHy7JgAZzTIAZNQC+Abnoz69UJFiIUAUQC2ABzC4AkuGjwk1RcroHdKAMJcQosJgRguUAMrAA5iDg+GETIALwWDEwgEADuyBwAdIlwUD6ifHAguADaALqCyBm4CnTyVsQQCAA2ySgIjs4FUFB8HM5QoD7IAD7IGCDlMKAk-LnF9OVVNch1TmDIukSk5JAAjHzoWHiEJGQUEMUzDYOVaiQroY1Q8cdqHAs7yxAr-HLIAPRvyNEeANbibR1clZxhVqsFDnN7ks9gAmdaYHAERa7SjSHT6IwmdRIA71OY3aAkGEXZJXAlQO7Ix4wl7vT7ELgQUQgADkc2+UB+wLoEzBtTx8ypewAzPDNkiHnspLJcbNkOSSMKSU1rsATtBKZLIMLaR8vr9-i5ATlubyphDBVqIAAWMWI7bQ1HIByzVzuLy+fyBIiyo5q07Ea3Ksn+jVQlE23X0xnMtn6zn0IA

💻 Code
interface TruthyPredicate {
    <T>(o: T): o is T & {};
}

interface EmptyInterface {
}

type ConstructorSignagure = {
    new (...args: any[]): any;
};

declare const arr: (string | undefined)[];

declare const predicate1: TruthyPredicate;
const filtered1 = arr.filter(predicate1); // works string[]


declare const predicate2: TruthyPredicate & EmptyInterface;
const filtered2 = arr.filter(predicate2); // doesn't work


declare const predicate3: TruthyPredicate & {} /* Empty object type instead of interface */;
const filtered3 = arr.filter(predicate3); // works string[]


declare const predicate4: TruthyPredicate & ConstructorSignagure;
const filtered4 = arr.filter(predicate4); // doesn't work
🙁 Actual behavior

Predicate stops working

🙂 Expected behavior

Predicate works

Additional information about the issue

I tried other scenarios

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 Playground repro and the Array.filter calls in the Code section, comparing predicate1 through predicate4. Investigate why intersecting the generic TruthyPredicate with EmptyInterface or ConstructorSignagure loses narrowing; done when the affected intersections narrow the filtered array as expected without breaking the additional generic cases described.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.