diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 4fa7d70e87547..b437fe006abd3 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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. diff --git a/tests/baselines/reference/genericCapturingFunctionNarrowing.js b/tests/baselines/reference/genericCapturingFunctionNarrowing.js index ee037bda04fe5..8a5fd504ea1b6 100644 --- a/tests/baselines/reference/genericCapturingFunctionNarrowing.js +++ b/tests/baselines/reference/genericCapturingFunctionNarrowing.js @@ -1,5 +1,5 @@ //// [genericCapturingFunctionNarrowing.ts] -function needsToNarrowTheType(thing: First | Second) { +function needsToNarrowTheType(thing: First | SubFirst | SubFirstMore | Second) { if (hasAFoo(thing)) { console.log(thing.foo); } diff --git a/tests/baselines/reference/genericCapturingFunctionNarrowing.symbols b/tests/baselines/reference/genericCapturingFunctionNarrowing.symbols index aa6ea24d8665e..5a77f325d16ac 100644 --- a/tests/baselines/reference/genericCapturingFunctionNarrowing.symbols +++ b/tests/baselines/reference/genericCapturingFunctionNarrowing.symbols @@ -1,24 +1,31 @@ === tests/cases/compiler/genericCapturingFunctionNarrowing.ts === -function needsToNarrowTheType(thing: First | Second) { +function needsToNarrowTheType(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 { @@ -28,7 +35,7 @@ function needsToNarrowTheTypeconsole : 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)) } diff --git a/tests/baselines/reference/genericCapturingFunctionNarrowing.types b/tests/baselines/reference/genericCapturingFunctionNarrowing.types index 66d1cd06d7f7b..d46f4540745c7 100644 --- a/tests/baselines/reference/genericCapturingFunctionNarrowing.types +++ b/tests/baselines/reference/genericCapturingFunctionNarrowing.types @@ -1,14 +1,15 @@ === tests/cases/compiler/genericCapturingFunctionNarrowing.ts === -function needsToNarrowTheType(thing: First | Second) { ->needsToNarrowTheType : (thing: First | Second) => void +function needsToNarrowTheType(thing: First | SubFirst | SubFirstMore | Second) { +>needsToNarrowTheType : (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 @@ -16,7 +17,7 @@ function needsToNarrowTheTypeconsole : Console >log : (...data: any[]) => void >thing.foo : string ->thing : First +>thing : First | SubFirst | SubFirstMore >foo : string } else { diff --git a/tests/cases/compiler/genericCapturingFunctionNarrowing.ts b/tests/cases/compiler/genericCapturingFunctionNarrowing.ts index bd6b86608c8cf..69b1ca05600a0 100644 --- a/tests/cases/compiler/genericCapturingFunctionNarrowing.ts +++ b/tests/cases/compiler/genericCapturingFunctionNarrowing.ts @@ -1,4 +1,4 @@ -function needsToNarrowTheType(thing: First | Second) { +function needsToNarrowTheType(thing: First | SubFirst | SubFirstMore | Second) { if (hasAFoo(thing)) { console.log(thing.foo); }