Skip to content

Commit

Permalink
Add test and fix for microsoft#44404
Browse files Browse the repository at this point in the history
  • Loading branch information
weswigham committed Jul 27, 2021
1 parent 21855df commit 795d244
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 25 deletions.
14 changes: 1 addition & 13 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24026,19 +24026,7 @@ namespace ts {

function getNarrowedType(type: Type, candidate: Type, assumeTrue: boolean, isRelated: (source: Type, target: Type) => boolean) {
if (!assumeTrue) {
return filterType(type, t => {
if (!isRelated(t, candidate)) {
return true;
}
if (candidate === t) {
return false;
}
const constraint = getBaseConstraintOfType(t);
if (constraint && constraint !== t) {
return !isRelated(constraint, candidate);
}
return false;
});
return filterType(type, t => !isRelated(t, candidate));
}
// If the current type is a union type, remove all constituents that couldn't be instances of
// the candidate type. If one or more constituents remain, return a union of those.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//// [genericCapturingFunctionNarrowing.ts]
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }>(thing: First | Second) {
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }, SubFirst extends First, SubFirstMore extends First & {other: string}>(thing: First | SubFirst | SubFirstMore | Second) {
if (hasAFoo(thing)) {
console.log(thing.foo);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
=== tests/cases/compiler/genericCapturingFunctionNarrowing.ts ===
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }>(thing: First | Second) {
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }, SubFirst extends First, SubFirstMore extends First & {other: string}>(thing: First | SubFirst | SubFirstMore | Second) {
>needsToNarrowTheType : Symbol(needsToNarrowTheType, Decl(genericCapturingFunctionNarrowing.ts, 0, 0))
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
>foo : Symbol(foo, Decl(genericCapturingFunctionNarrowing.ts, 0, 45))
>Second : Symbol(Second, Decl(genericCapturingFunctionNarrowing.ts, 0, 60))
>bar : Symbol(bar, Decl(genericCapturingFunctionNarrowing.ts, 0, 77))
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 93))
>SubFirst : Symbol(SubFirst, Decl(genericCapturingFunctionNarrowing.ts, 0, 92))
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
>SubFirstMore : Symbol(SubFirstMore, Decl(genericCapturingFunctionNarrowing.ts, 0, 116))
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
>other : Symbol(other, Decl(genericCapturingFunctionNarrowing.ts, 0, 147))
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 163))
>First : Symbol(First, Decl(genericCapturingFunctionNarrowing.ts, 0, 30))
>SubFirst : Symbol(SubFirst, Decl(genericCapturingFunctionNarrowing.ts, 0, 92))
>SubFirstMore : Symbol(SubFirstMore, Decl(genericCapturingFunctionNarrowing.ts, 0, 116))
>Second : Symbol(Second, Decl(genericCapturingFunctionNarrowing.ts, 0, 60))

if (hasAFoo(thing)) {
>hasAFoo : Symbol(hasAFoo, Decl(genericCapturingFunctionNarrowing.ts, 7, 5))
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 93))
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 163))

console.log(thing.foo);
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>thing.foo : Symbol(foo, Decl(genericCapturingFunctionNarrowing.ts, 0, 45))
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 93))
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 163))
>foo : Symbol(foo, Decl(genericCapturingFunctionNarrowing.ts, 0, 45))
}
else {
Expand All @@ -28,7 +35,7 @@ function needsToNarrowTheType<First extends { foo: string }, Second extends { ba
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
>thing.bar : Symbol(bar, Decl(genericCapturingFunctionNarrowing.ts, 0, 77))
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 93))
>thing : Symbol(thing, Decl(genericCapturingFunctionNarrowing.ts, 0, 163))
>bar : Symbol(bar, Decl(genericCapturingFunctionNarrowing.ts, 0, 77))
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
=== tests/cases/compiler/genericCapturingFunctionNarrowing.ts ===
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }>(thing: First | Second) {
>needsToNarrowTheType : <First extends { foo: string; }, Second extends { bar: string; }>(thing: First | Second) => void
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }, SubFirst extends First, SubFirstMore extends First & {other: string}>(thing: First | SubFirst | SubFirstMore | Second) {
>needsToNarrowTheType : <First extends { foo: string; }, Second extends { bar: string; }, SubFirst extends First, SubFirstMore extends First & { other: string; }>(thing: First | SubFirst | SubFirstMore | Second) => void
>foo : string
>bar : string
>thing : First | Second
>other : string
>thing : First | Second | SubFirst | SubFirstMore

if (hasAFoo(thing)) {
>hasAFoo(thing) : boolean
>hasAFoo : (value: First | Second) => value is First
>thing : First | Second
>thing : First | Second | SubFirst | SubFirstMore

console.log(thing.foo);
>console.log(thing.foo) : void
>console.log : (...data: any[]) => void
>console : Console
>log : (...data: any[]) => void
>thing.foo : string
>thing : First
>thing : First | SubFirst | SubFirstMore
>foo : string
}
else {
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/compiler/genericCapturingFunctionNarrowing.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }>(thing: First | Second) {
function needsToNarrowTheType<First extends { foo: string }, Second extends { bar: string }, SubFirst extends First, SubFirstMore extends First & {other: string}>(thing: First | SubFirst | SubFirstMore | Second) {
if (hasAFoo(thing)) {
console.log(thing.foo);
}
Expand Down

0 comments on commit 795d244

Please sign in to comment.