Skip to content

Commit 795d244

Browse files
committed
Add test and fix for #44404
1 parent 21855df commit 795d244

File tree

5 files changed

+21
-25
lines changed

5 files changed

+21
-25
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24026,19 +24026,7 @@ namespace ts {
2402624026

2402724027
function getNarrowedType(type: Type, candidate: Type, assumeTrue: boolean, isRelated: (source: Type, target: Type) => boolean) {
2402824028
if (!assumeTrue) {
24029-
return filterType(type, t => {
24030-
if (!isRelated(t, candidate)) {
24031-
return true;
24032-
}
24033-
if (candidate === t) {
24034-
return false;
24035-
}
24036-
const constraint = getBaseConstraintOfType(t);
24037-
if (constraint && constraint !== t) {
24038-
return !isRelated(constraint, candidate);
24039-
}
24040-
return false;
24041-
});
24029+
return filterType(type, t => !isRelated(t, candidate));
2404224030
}
2404324031
// If the current type is a union type, remove all constituents that couldn't be instances of
2404424032
// the candidate type. If one or more constituents remain, return a union of those.

tests/baselines/reference/genericCapturingFunctionNarrowing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//// [genericCapturingFunctionNarrowing.ts]
2-
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }>(thing: First | Second) {
2+
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }, SubFirst extends First, SubFirstMore extends First & {other: string}>(thing: First | SubFirst | SubFirstMore | Second) {
33
if (hasAFoo(thing)) {
44
console.log(thing.foo);
55
}

tests/baselines/reference/genericCapturingFunctionNarrowing.symbols

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,31 @@
11
=== tests/cases/compiler/genericCapturingFunctionNarrowing.ts ===
2-
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }>(thing: First | Second) {
2+
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }, SubFirst extends First, SubFirstMore extends First & {other: string}>(thing: First | SubFirst | SubFirstMore | Second) {
33
>needsToNarrowTheType : Symbol(needsToNarrowTheType, Decl(genericCapturingFunctionNarrowing.ts, 0, 0))
44
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
55
>foo : Symbol(foo, Decl(genericCapturingFunctionNarrowing.ts, 0, 45))
66
>Second : Symbol(Second, Decl(genericCapturingFunctionNarrowing.ts, 0, 60))
77
>bar : Symbol(bar, Decl(genericCapturingFunctionNarrowing.ts, 0, 77))
8-
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 93))
8+
>SubFirst : Symbol(SubFirst, Decl(genericCapturingFunctionNarrowing.ts, 0, 92))
99
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
10+
>SubFirstMore : Symbol(SubFirstMore, Decl(genericCapturingFunctionNarrowing.ts, 0, 116))
11+
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
12+
>other : Symbol(other, Decl(genericCapturingFunctionNarrowing.ts, 0, 147))
13+
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 163))
14+
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
15+
>SubFirst : Symbol(SubFirst, Decl(genericCapturingFunctionNarrowing.ts, 0, 92))
16+
>SubFirstMore : Symbol(SubFirstMore, Decl(genericCapturingFunctionNarrowing.ts, 0, 116))
1017
>Second : Symbol(Second, Decl(genericCapturingFunctionNarrowing.ts, 0, 60))
1118

1219
if (hasAFoo(thing)) {
1320
>hasAFoo : Symbol(hasAFoo, Decl(genericCapturingFunctionNarrowing.ts, 7, 5))
14-
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 93))
21+
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 163))
1522

1623
console.log(thing.foo);
1724
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
1825
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
1926
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
2027
>thing.foo : Symbol(foo, Decl(genericCapturingFunctionNarrowing.ts, 0, 45))
21-
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 93))
28+
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 163))
2229
>foo : Symbol(foo, Decl(genericCapturingFunctionNarrowing.ts, 0, 45))
2330
}
2431
else {
@@ -28,7 +35,7 @@ function needsToNarrowTheType<First extends { foo: string }, Second extends { ba
2835
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
2936
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
3037
>thing.bar : Symbol(bar, Decl(genericCapturingFunctionNarrowing.ts, 0, 77))
31-
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 93))
38+
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 163))
3239
>bar : Symbol(bar, Decl(genericCapturingFunctionNarrowing.ts, 0, 77))
3340
}
3441

tests/baselines/reference/genericCapturingFunctionNarrowing.types

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
=== tests/cases/compiler/genericCapturingFunctionNarrowing.ts ===
2-
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }>(thing: First | Second) {
3-
>needsToNarrowTheType : <First extends { foo: string; }, Second extends { bar: string; }>(thing: First | Second) => void
2+
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }, SubFirst extends First, SubFirstMore extends First & {other: string}>(thing: First | SubFirst | SubFirstMore | Second) {
3+
>needsToNarrowTheType : <First extends { foo: string; }, Second extends { bar: string; }, SubFirst extends First, SubFirstMore extends First & { other: string; }>(thing: First | SubFirst | SubFirstMore | Second) => void
44
>foo : string
55
>bar : string
6-
>thing : First | Second
6+
>other : string
7+
>thing : First | Second | SubFirst | SubFirstMore
78

89
if (hasAFoo(thing)) {
910
>hasAFoo(thing) : boolean
1011
>hasAFoo : (value: First | Second) => value is First
11-
>thing : First | Second
12+
>thing : First | Second | SubFirst | SubFirstMore
1213

1314
console.log(thing.foo);
1415
>console.log(thing.foo) : void
1516
>console.log : (...data: any[]) => void
1617
>console : Console
1718
>log : (...data: any[]) => void
1819
>thing.foo : string
19-
>thing : First
20+
>thing : First | SubFirst | SubFirstMore
2021
>foo : string
2122
}
2223
else {

tests/cases/compiler/genericCapturingFunctionNarrowing.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }>(thing: First | Second) {
1+
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }, SubFirst extends First, SubFirstMore extends First & {other: string}>(thing: First | SubFirst | SubFirstMore | Second) {
22
if (hasAFoo(thing)) {
33
console.log(thing.foo);
44
}

0 commit comments

Comments
 (0)