Skip to content

Commit 4ae2a07

Browse files
committed
update tests
1 parent e697e21 commit 4ae2a07

12 files changed

+109
-219
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24098,7 +24098,7 @@ namespace ts {
2409824098
const narrowedPropType = narrowTypeCb(bigUnion);
2409924099
const markSet = new Set<TypeId>();
2410024100
propertyTypeArray.forEach(propertyType => {
24101-
if (propertyType.finalType && !(propertyType.finalType.flags & TypeFlags.Never) && isTypeComparableTo(propertyType.finalType, narrowedPropType)) {
24101+
if (propertyType.finalType && !(narrowedPropType.flags & TypeFlags.Never) && isTypeComparableTo(narrowedPropType, propertyType.finalType)) {
2410224102
markSet.add(propertyType.typeId);
2410324103
}
2410424104
});

tests/baselines/reference/controlFlowOptionalChain.errors.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,10 @@ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(463,9): error TS
5757
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(498,13): error TS2532: Object is possibly 'undefined'.
5858
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(501,13): error TS2532: Object is possibly 'undefined'.
5959
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(515,13): error TS2532: Object is possibly 'undefined'.
60-
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(518,13): error TS2532: Object is possibly 'undefined'.
6160
tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(567,21): error TS2532: Object is possibly 'undefined'.
6261

6362

64-
==== tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts (61 errors) ====
63+
==== tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts (60 errors) ====
6564
// assignments in shortcutting chain
6665
declare const o: undefined | {
6766
[key: string]: any;
@@ -697,9 +696,7 @@ tests/cases/conformance/controlFlow/controlFlowOptionalChain.ts(567,21): error T
697696
!!! error TS2532: Object is possibly 'undefined'.
698697
break;
699698
default:
700-
o.foo; // Error
701-
~
702-
!!! error TS2532: Object is possibly 'undefined'.
699+
o.foo;
703700
break;
704701
}
705702
}

tests/baselines/reference/controlFlowOptionalChain.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ function f41(o: Thing | undefined) {
516516
o.foo; // Error
517517
break;
518518
default:
519-
o.foo; // Error
519+
o.foo;
520520
break;
521521
}
522522
}
@@ -1049,7 +1049,7 @@ function f41(o) {
10491049
o.foo; // Error
10501050
break;
10511051
default:
1052-
o.foo; // Error
1052+
o.foo;
10531053
break;
10541054
}
10551055
}

tests/baselines/reference/controlFlowOptionalChain.symbols

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,9 +1438,7 @@ function f23(o: Thing | undefined) {
14381438
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
14391439

14401440
o.foo; // Error
1441-
>o.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
14421441
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 439, 13))
1443-
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
14441442
}
14451443
else {
14461444
o.foo;
@@ -1460,19 +1458,15 @@ function f23(o: Thing | undefined) {
14601458
}
14611459
else {
14621460
o.foo; // Error
1463-
>o.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
14641461
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 439, 13))
1465-
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
14661462
}
14671463
if (typeof o?.foo == "undefined") {
14681464
>o?.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
14691465
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 439, 13))
14701466
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
14711467

14721468
o.foo; // Error
1473-
>o.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
14741469
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 439, 13))
1475-
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
14761470
}
14771471
else {
14781472
o.foo;
@@ -1492,9 +1486,7 @@ function f23(o: Thing | undefined) {
14921486
}
14931487
else {
14941488
o.foo; // Error
1495-
>o.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
14961489
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 439, 13))
1497-
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
14981490
}
14991491
}
15001492

@@ -1636,13 +1628,11 @@ function f41(o: Thing | undefined) {
16361628
break;
16371629
case "undefined":
16381630
o.foo; // Error
1639-
>o.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
16401631
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 505, 13))
1641-
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
16421632

16431633
break;
16441634
default:
1645-
o.foo; // Error
1635+
o.foo;
16461636
>o.foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))
16471637
>o : Symbol(o, Decl(controlFlowOptionalChain.ts, 505, 13))
16481638
>foo : Symbol(foo, Decl(controlFlowOptionalChain.ts, 161, 14))

tests/baselines/reference/controlFlowOptionalChain.types

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,9 +1618,9 @@ function f23(o: Thing | undefined) {
16181618
>"undefined" : "undefined"
16191619

16201620
o.foo; // Error
1621-
>o.foo : never
1622-
>o : Thing | undefined
1623-
>foo : never
1621+
>o.foo : any
1622+
>o : undefined
1623+
>foo : any
16241624
}
16251625
else {
16261626
o.foo;
@@ -1643,9 +1643,9 @@ function f23(o: Thing | undefined) {
16431643
}
16441644
else {
16451645
o.foo; // Error
1646-
>o.foo : never
1647-
>o : Thing | undefined
1648-
>foo : never
1646+
>o.foo : any
1647+
>o : undefined
1648+
>foo : any
16491649
}
16501650
if (typeof o?.foo == "undefined") {
16511651
>typeof o?.foo == "undefined" : boolean
@@ -1656,9 +1656,9 @@ function f23(o: Thing | undefined) {
16561656
>"undefined" : "undefined"
16571657

16581658
o.foo; // Error
1659-
>o.foo : never
1660-
>o : Thing | undefined
1661-
>foo : never
1659+
>o.foo : any
1660+
>o : undefined
1661+
>foo : any
16621662
}
16631663
else {
16641664
o.foo;
@@ -1681,9 +1681,9 @@ function f23(o: Thing | undefined) {
16811681
}
16821682
else {
16831683
o.foo; // Error
1684-
>o.foo : never
1685-
>o : Thing | undefined
1686-
>foo : never
1684+
>o.foo : any
1685+
>o : undefined
1686+
>foo : any
16871687
}
16881688
}
16891689

@@ -1852,15 +1852,15 @@ function f41(o: Thing | undefined) {
18521852
>"undefined" : "undefined"
18531853

18541854
o.foo; // Error
1855-
>o.foo : never
1856-
>o : Thing | undefined
1857-
>foo : never
1855+
>o.foo : any
1856+
>o : undefined
1857+
>foo : any
18581858

18591859
break;
18601860
default:
1861-
o.foo; // Error
1861+
o.foo;
18621862
>o.foo : never
1863-
>o : Thing | undefined
1863+
>o : Thing
18641864
>foo : never
18651865

18661866
break;

tests/baselines/reference/discriminatedUnionTypes2.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(27,30): error TS
22
Object literal may only specify known properties, and 'c' does not exist in type '{ a: null; b: string; }'.
33
tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(32,11): error TS2339: Property 'b' does not exist on type '{ a: 0; b: string; } | { a: T; c: number; }'.
44
Property 'b' does not exist on type '{ a: T; c: number; }'.
5-
tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(132,11): error TS2339: Property 'value' does not exist on type 'never'.
65

76

8-
==== tests/cases/conformance/types/union/discriminatedUnionTypes2.ts (3 errors) ====
7+
==== tests/cases/conformance/types/union/discriminatedUnionTypes2.ts (2 errors) ====
98
function f10(x : { kind: false, a: string } | { kind: true, b: string } | { kind: string, c: string }) {
109
if (x.kind === false) {
1110
x.a;
@@ -144,8 +143,6 @@ tests/cases/conformance/types/union/discriminatedUnionTypes2.ts(132,11): error T
144143
}
145144
else {
146145
x.value; // Error, x is never
147-
~~~~~
148-
!!! error TS2339: Property 'value' does not exist on type 'never'.
149146
}
150147
}
151148

tests/baselines/reference/discriminatedUnionTypes2.symbols

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,9 @@ function foo1(x: RuntimeValue & { type: 'number' }) {
367367
}
368368
else {
369369
x.value; // Error, x is never
370+
>x.value : Symbol(value, Decl(discriminatedUnionTypes2.ts, 122, 23))
370371
>x : Symbol(x, Decl(discriminatedUnionTypes2.ts, 126, 14))
372+
>value : Symbol(value, Decl(discriminatedUnionTypes2.ts, 122, 23))
371373
}
372374
}
373375

tests/baselines/reference/discriminatedUnionTypes2.types

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,9 @@ function foo1(x: RuntimeValue & { type: 'number' }) {
380380
}
381381
else {
382382
x.value; // Error, x is never
383-
>x.value : any
384-
>x : never
385-
>value : any
383+
>x.value : number
384+
>x : { type: "number"; value: number; } & { type: "number"; }
385+
>value : number
386386
}
387387
}
388388

tests/baselines/reference/discriminatedUnionTypes3.errors.txt

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
1-
tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(59,11): error TS2339: Property 's' does not exist on type 'T'.
1+
tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(13,15): error TS2339: Property 'x' does not exist on type 'X | Y'.
2+
Property 'x' does not exist on type 'Y'.
3+
tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(16,15): error TS2339: Property 'y' does not exist on type 'X | Y'.
4+
Property 'y' does not exist on type 'X'.
5+
tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(34,11): error TS2339: Property 'x' does not exist on type 'X | Y'.
6+
Property 'x' does not exist on type 'Y'.
7+
tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(36,11): error TS2339: Property 'y' does not exist on type 'X | Y'.
8+
Property 'y' does not exist on type 'X'.
9+
tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(42,11): error TS2339: Property 'x' does not exist on type 'X | W'.
10+
Property 'x' does not exist on type 'W'.
11+
tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(55,11): error TS2339: Property 's' does not exist on type 'S | T'.
12+
Property 's' does not exist on type 'T'.
13+
tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(56,21): error TS2339: Property 'x' does not exist on type 'X | Y'.
14+
Property 'x' does not exist on type 'Y'.
15+
tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(59,11): error TS2339: Property 's' does not exist on type 'S | T'.
16+
Property 's' does not exist on type 'T'.
217

318

4-
==== tests/cases/conformance/types/union/discriminatedUnionTypes3.ts (1 errors) ====
19+
==== tests/cases/conformance/types/union/discriminatedUnionTypes3.ts (8 errors) ====
520
type A = { type2: "a", a: number }
621
type B = { type2: "b", b: number }
722
type C = { type2: "c", b: number | string }
@@ -15,9 +30,15 @@ tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(59,11): error TS
1530
switch (x.type1.type2) {
1631
case "a":
1732
x.x // typeof x is X
33+
~
34+
!!! error TS2339: Property 'x' does not exist on type 'X | Y'.
35+
!!! error TS2339: Property 'x' does not exist on type 'Y'.
1836
break;
1937
case "b":
2038
x.y // typeof x is Y
39+
~
40+
!!! error TS2339: Property 'y' does not exist on type 'X | Y'.
41+
!!! error TS2339: Property 'y' does not exist on type 'X'.
2142
break;
2243
}
2344
}
@@ -36,14 +57,23 @@ tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(59,11): error TS
3657
function f34(x: X | Y) {
3758
if (x.type1.type2 === "a") {
3859
x.x // typeof x is X
60+
~
61+
!!! error TS2339: Property 'x' does not exist on type 'X | Y'.
62+
!!! error TS2339: Property 'x' does not exist on type 'Y'.
3963
} else if (x.type1.type2 === "b") {
4064
x.y // typeof x is Y
65+
~
66+
!!! error TS2339: Property 'y' does not exist on type 'X | Y'.
67+
!!! error TS2339: Property 'y' does not exist on type 'X'.
4168
}
4269
}
4370

4471
function f35(x: X | W) {
4572
if (x.type1?.type2 === "a") {
4673
x.x
74+
~
75+
!!! error TS2339: Property 'x' does not exist on type 'X | W'.
76+
!!! error TS2339: Property 'x' does not exist on type 'W'.
4777
}
4878
}
4979

@@ -57,12 +87,37 @@ tests/cases/conformance/types/union/discriminatedUnionTypes3.ts(59,11): error TS
5787
function f37(s: S | T) {
5888
if (s.sub.type0.type1.type2 === "a") {
5989
s.s // typeof s is S
90+
~
91+
!!! error TS2339: Property 's' does not exist on type 'S | T'.
92+
!!! error TS2339: Property 's' does not exist on type 'T'.
6093
s.sub.type0.x // type of s.sub.type is X
94+
~
95+
!!! error TS2339: Property 'x' does not exist on type 'X | Y'.
96+
!!! error TS2339: Property 'x' does not exist on type 'Y'.
6197
s.sub.type0.type1.a // type of s.sub.type.type is A
6298
} else {
6399
s.s // type error!
64100
~
65-
!!! error TS2339: Property 's' does not exist on type 'T'.
101+
!!! error TS2339: Property 's' does not exist on type 'S | T'.
102+
!!! error TS2339: Property 's' does not exist on type 'T'.
66103
}
67104
}
105+
106+
// Repro from #44435
107+
108+
type Correct = {
109+
code: string
110+
property: true
111+
err: undefined
112+
}
113+
type Err = {
114+
err: `${string} is wrong!`
115+
}
116+
type SomeReturnType = Correct | Err;
117+
118+
const example: SomeReturnType = {} as SomeReturnType;
119+
120+
if (example.err === undefined) {
121+
example.property; // true
122+
}
68123

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//// [typeGuardsInProperties.ts]
2-
// Note that type guards affect types of variables and parameters only and
3-
// have no effect on members of objects such as properties.
2+
// Note that type guards affect types of variables and parameters only and
3+
// have no effect on members of objects such as properties.
44

55
var num: number;
66
var strOrNum: string | number;
@@ -15,26 +15,19 @@ class C1 {
1515
strOrNum = typeof this.pp1 === "string" && this.pp1; // string | number
1616
strOrNum = typeof this.pp2 === "string" && this.pp2; // string | number
1717
strOrNum = typeof this.pp3 === "string" && this.pp3; // string | number
18-
strOrNum = typeof this.pp1 !== "string" && this.pp1; // string | number
19-
strOrNum = typeof this.pp2 !== "string" && this.pp2; // string | number
20-
strOrNum = typeof this.pp3 !== "string" && this.pp3; // string | number
2118
}
2219
}
2320
var c1: C1;
2421
strOrNum = typeof c1.pp2 === "string" && c1.pp2; // string | number
2522
strOrNum = typeof c1.pp3 === "string" && c1.pp3; // string | number
26-
strOrNum = typeof c1.pp2 !== "string" && c1.pp2; // string | number
27-
strOrNum = typeof c1.pp3 !== "string" && c1.pp3; // string | number
2823
var obj1: {
2924
x: string | number;
3025
};
31-
strOrNum = typeof obj1.x === "string" && obj1.x; // string | number
32-
strOrNum = typeof obj1.x !== "string" && obj1.x; // string | number
33-
26+
strOrNum = typeof obj1.x === "string" && obj1.x; // string | number
3427

3528
//// [typeGuardsInProperties.js]
36-
// Note that type guards affect types of variables and parameters only and
37-
// have no effect on members of objects such as properties.
29+
// Note that type guards affect types of variables and parameters only and
30+
// have no effect on members of objects such as properties.
3831
var num;
3932
var strOrNum;
4033
var C1 = /** @class */ (function () {
@@ -52,17 +45,11 @@ var C1 = /** @class */ (function () {
5245
strOrNum = typeof this.pp1 === "string" && this.pp1; // string | number
5346
strOrNum = typeof this.pp2 === "string" && this.pp2; // string | number
5447
strOrNum = typeof this.pp3 === "string" && this.pp3; // string | number
55-
strOrNum = typeof this.pp1 !== "string" && this.pp1; // string | number
56-
strOrNum = typeof this.pp2 !== "string" && this.pp2; // string | number
57-
strOrNum = typeof this.pp3 !== "string" && this.pp3; // string | number
5848
};
5949
return C1;
6050
}());
6151
var c1;
6252
strOrNum = typeof c1.pp2 === "string" && c1.pp2; // string | number
6353
strOrNum = typeof c1.pp3 === "string" && c1.pp3; // string | number
64-
strOrNum = typeof c1.pp2 !== "string" && c1.pp2; // string | number
65-
strOrNum = typeof c1.pp3 !== "string" && c1.pp3; // string | number
6654
var obj1;
6755
strOrNum = typeof obj1.x === "string" && obj1.x; // string | number
68-
strOrNum = typeof obj1.x !== "string" && obj1.x; // string | number

0 commit comments

Comments
 (0)