Skip to content

Commit 343f3df

Browse files
committed
Remove assignability cases in getNarrowedType
1 parent 049618f commit 343f3df

11 files changed

+35
-51
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17551,14 +17551,8 @@ namespace ts {
1755117551
}
1755217552
}
1755317553
// If the candidate type is a subtype of the target type, narrow to the candidate type.
17554-
// Otherwise, if the target type is assignable to the candidate type, keep the target type.
17555-
// Otherwise, if the candidate type is assignable to the target type, narrow to the candidate
17556-
// type. Otherwise, the types are completely unrelated, so narrow to an intersection of the
17557-
// two types.
17558-
return isTypeSubtypeOf(candidate, type) ? candidate :
17559-
isTypeAssignableTo(type, candidate) ? type :
17560-
isTypeAssignableTo(candidate, type) ? candidate :
17561-
getIntersectionType([type, candidate]);
17554+
// Otherwise, narrow to an intersection of the two types.
17555+
return isTypeSubtypeOf(candidate, type) ? candidate : getIntersectionType([type, candidate]);
1756217556
}
1756317557

1756417558
function narrowTypeByTypePredicate(type: Type, callExpression: CallExpression, assumeTrue: boolean): Type {

tests/baselines/reference/controlFlowInstanceof.symbols

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ function f1(s: Set<string> | Set<number>) {
2525
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 2, 12))
2626

2727
s.add(42);
28-
>s.add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
28+
>s.add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
2929
>s : Symbol(s, Decl(controlFlowInstanceof.ts, 2, 12))
30-
>add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --))
30+
>add : Symbol(Set.add, Decl(lib.es2015.collection.d.ts, --, --), Decl(lib.es2015.collection.d.ts, --, --))
3131
}
3232

3333
function f2(s: Set<string> | Set<number>) {

tests/baselines/reference/controlFlowInstanceof.types

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ function f1(s: Set<string> | Set<number>) {
2020
>Set : SetConstructor
2121

2222
s; // Set<number>
23-
>s : Set<number>
23+
>s : Set<number> & Set<any>
2424
}
2525
s; // Set<number>
26-
>s : Set<number>
26+
>s : Set<number> & Set<any>
2727

2828
s.add(42);
29-
>s.add(42) : Set<number>
30-
>s.add : (value: number) => Set<number>
31-
>s : Set<number>
32-
>add : (value: number) => Set<number>
29+
>s.add(42) : Set<number> & Set<any>
30+
>s.add : ((value: number) => Set<number> & Set<any>) & ((value: any) => Set<number> & Set<any>)
31+
>s : Set<number> & Set<any>
32+
>add : ((value: number) => Set<number> & Set<any>) & ((value: any) => Set<number> & Set<any>)
3333
>42 : 42
3434
}
3535

@@ -105,7 +105,7 @@ function f4(s: Set<string> | Set<number>) {
105105
>Set : SetConstructor
106106

107107
s; // Set<number>
108-
>s : Set<number>
108+
>s : Set<number> & Set<any>
109109
}
110110
else {
111111
s; // never

tests/baselines/reference/instanceOfAssignability.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ function fn2(x: Base) {
7070
// 1.5: y: Base
7171
// Want: y: Derived1
7272
let y = x;
73-
>y : Derived1
74-
>x : Derived1
73+
>y : Base & Derived1
74+
>x : Base & Derived1
7575
}
7676
}
7777

@@ -104,8 +104,8 @@ function fn4(x: Base|Derived2) {
104104
// 1.5: y: {}
105105
// Want: Derived1
106106
let y = x;
107-
>y : Derived1
108-
>x : Derived1
107+
>y : (Base & Derived1) | (Derived2 & Derived1)
108+
>x : (Base & Derived1) | (Derived2 & Derived1)
109109
}
110110
}
111111

tests/baselines/reference/instanceofWithPrimitiveUnion.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ function test1(x: number | string) {
99
>Object : ObjectConstructor
1010

1111
x;
12-
>x : string | number
12+
>x : (string & Object) | (number & Object)
1313
}
1414
}
1515

@@ -23,7 +23,7 @@ function test2(x: (number | string) | number) {
2323
>Object : ObjectConstructor
2424

2525
x;
26-
>x : string | number
26+
>x : (string & Object) | (number & Object)
2727
}
2828
}
2929

tests/baselines/reference/narrowingConstrainedTypeParameter.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ export function speak<TPet extends Pet>(pet: TPet, voice: (pet: TPet) => string)
3939
return voice(pet);
4040
>voice(pet) : string
4141
>voice : (pet: TPet) => string
42-
>pet : TPet
42+
>pet : TPet & Pet
4343
}

tests/baselines/reference/typeGuardFunction.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ if(isA(subType)) {
6565

6666
subType.propC;
6767
>subType.propC : number
68-
>subType : C
68+
>subType : C & A
6969
>propC : number
7070
}
7171

tests/baselines/reference/typeGuardIntersectionTypes.symbols

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -176,37 +176,37 @@ function identifyBeast(beast: Beast) {
176176
>beast : Symbol(beast, Decl(typeGuardIntersectionTypes.ts, 64, 23))
177177

178178
if (beast.legs === 4) {
179-
>beast.legs : Symbol(Legged.legs, Decl(typeGuardIntersectionTypes.ts, 56, 21))
179+
>beast.legs : Symbol(legs, Decl(typeGuardIntersectionTypes.ts, 55, 38), Decl(typeGuardIntersectionTypes.ts, 56, 21))
180180
>beast : Symbol(beast, Decl(typeGuardIntersectionTypes.ts, 64, 23))
181-
>legs : Symbol(Legged.legs, Decl(typeGuardIntersectionTypes.ts, 56, 21))
181+
>legs : Symbol(legs, Decl(typeGuardIntersectionTypes.ts, 55, 38), Decl(typeGuardIntersectionTypes.ts, 56, 21))
182182

183183
log(`pegasus - 4 legs, wings`);
184184
>log : Symbol(log, Decl(typeGuardIntersectionTypes.ts, 48, 1))
185185
}
186186
else if (beast.legs === 2) {
187-
>beast.legs : Symbol(Legged.legs, Decl(typeGuardIntersectionTypes.ts, 56, 21))
187+
>beast.legs : Symbol(legs, Decl(typeGuardIntersectionTypes.ts, 55, 38), Decl(typeGuardIntersectionTypes.ts, 56, 21))
188188
>beast : Symbol(beast, Decl(typeGuardIntersectionTypes.ts, 64, 23))
189-
>legs : Symbol(Legged.legs, Decl(typeGuardIntersectionTypes.ts, 56, 21))
189+
>legs : Symbol(legs, Decl(typeGuardIntersectionTypes.ts, 55, 38), Decl(typeGuardIntersectionTypes.ts, 56, 21))
190190

191191
log(`bird - 2 legs, wings`);
192192
>log : Symbol(log, Decl(typeGuardIntersectionTypes.ts, 48, 1))
193193
}
194194
else {
195195
log(`unknown - ${beast.legs} legs, wings`);
196196
>log : Symbol(log, Decl(typeGuardIntersectionTypes.ts, 48, 1))
197-
>beast.legs : Symbol(Legged.legs, Decl(typeGuardIntersectionTypes.ts, 56, 21))
197+
>beast.legs : Symbol(legs, Decl(typeGuardIntersectionTypes.ts, 55, 38), Decl(typeGuardIntersectionTypes.ts, 56, 21))
198198
>beast : Symbol(beast, Decl(typeGuardIntersectionTypes.ts, 64, 23))
199-
>legs : Symbol(Legged.legs, Decl(typeGuardIntersectionTypes.ts, 56, 21))
199+
>legs : Symbol(legs, Decl(typeGuardIntersectionTypes.ts, 55, 38), Decl(typeGuardIntersectionTypes.ts, 56, 21))
200200
}
201201
}
202202

203203
// All non-winged beasts with legs
204204
else {
205205
log(`manbearpig - ${beast.legs} legs, no wings`);
206206
>log : Symbol(log, Decl(typeGuardIntersectionTypes.ts, 48, 1))
207-
>beast.legs : Symbol(Legged.legs, Decl(typeGuardIntersectionTypes.ts, 56, 21))
207+
>beast.legs : Symbol(legs, Decl(typeGuardIntersectionTypes.ts, 55, 38), Decl(typeGuardIntersectionTypes.ts, 56, 21))
208208
>beast : Symbol(beast, Decl(typeGuardIntersectionTypes.ts, 64, 23))
209-
>legs : Symbol(Legged.legs, Decl(typeGuardIntersectionTypes.ts, 56, 21))
209+
>legs : Symbol(legs, Decl(typeGuardIntersectionTypes.ts, 55, 38), Decl(typeGuardIntersectionTypes.ts, 56, 21))
210210
}
211211
}
212212

tests/baselines/reference/typeGuardIntersectionTypes.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,12 @@ function identifyBeast(beast: Beast) {
166166
if (hasWings(beast)) {
167167
>hasWings(beast) : boolean
168168
>hasWings : (x: Beast) => x is Winged
169-
>beast : Legged
169+
>beast : Beast & Legged
170170

171171
if (beast.legs === 4) {
172172
>beast.legs === 4 : boolean
173173
>beast.legs : number
174-
>beast : Legged & Winged
174+
>beast : Beast & Legged & Winged
175175
>legs : number
176176
>4 : 4
177177

@@ -183,7 +183,7 @@ function identifyBeast(beast: Beast) {
183183
else if (beast.legs === 2) {
184184
>beast.legs === 2 : boolean
185185
>beast.legs : number
186-
>beast : Legged & Winged
186+
>beast : Beast & Legged & Winged
187187
>legs : number
188188
>2 : 2
189189

@@ -198,7 +198,7 @@ function identifyBeast(beast: Beast) {
198198
>log : (s: string) => void
199199
>`unknown - ${beast.legs} legs, wings` : string
200200
>beast.legs : number
201-
>beast : Legged & Winged
201+
>beast : Beast & Legged & Winged
202202
>legs : number
203203
}
204204
}
@@ -210,7 +210,7 @@ function identifyBeast(beast: Beast) {
210210
>log : (s: string) => void
211211
>`manbearpig - ${beast.legs} legs, no wings` : string
212212
>beast.legs : number
213-
>beast : Legged
213+
>beast : Beast & Legged
214214
>legs : number
215215
}
216216
}

tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.errors.txt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,14 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstru
1515
Property 'bar1' does not exist on type 'E2'.
1616
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(119,11): error TS2339: Property 'bar2' does not exist on type 'E1 | E2'.
1717
Property 'bar2' does not exist on type 'E1'.
18-
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(134,11): error TS2339: Property 'foo' does not exist on type 'string | F'.
19-
Property 'foo' does not exist on type 'string'.
20-
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(135,11): error TS2339: Property 'bar' does not exist on type 'string | F'.
21-
Property 'bar' does not exist on type 'string'.
2218
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(160,11): error TS2339: Property 'foo2' does not exist on type 'G1'.
2319
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(166,11): error TS2339: Property 'foo2' does not exist on type 'G1'.
2420
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(182,11): error TS2339: Property 'bar' does not exist on type 'H'.
2521
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(187,11): error TS2551: Property 'foo1' does not exist on type 'H'. Did you mean 'foo'?
2622
tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts(188,11): error TS2551: Property 'foo2' does not exist on type 'H'. Did you mean 'foo'?
2723

2824

29-
==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts (20 errors) ====
25+
==== tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts (18 errors) ====
3026
interface AConstructor {
3127
new (): A;
3228
}
@@ -191,13 +187,7 @@ tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstru
191187
var obj11: F | string;
192188
if (obj11 instanceof F) { // can't type narrowing, construct signature returns any.
193189
obj11.foo;
194-
~~~
195-
!!! error TS2339: Property 'foo' does not exist on type 'string | F'.
196-
!!! error TS2339: Property 'foo' does not exist on type 'string'.
197190
obj11.bar;
198-
~~~
199-
!!! error TS2339: Property 'bar' does not exist on type 'string | F'.
200-
!!! error TS2339: Property 'bar' does not exist on type 'string'.
201191
}
202192

203193
var obj12: any;

tests/baselines/reference/typeGuardsWithInstanceOfByConstructorSignature.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,12 @@ if (obj11 instanceof F) { // can't type narrowing, construct signature returns a
341341

342342
obj11.foo;
343343
>obj11.foo : any
344-
>obj11 : string | F
344+
>obj11 : any
345345
>foo : any
346346

347347
obj11.bar;
348348
>obj11.bar : any
349-
>obj11 : string | F
349+
>obj11 : any
350350
>bar : any
351351
}
352352

0 commit comments

Comments
 (0)