|
1 |
| -tests/cases/conformance/controlFlow/controlFlowAliasing.ts(61,13): error TS2339: Property 'foo' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
| 1 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(59,13): error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 2 | + Type 'number' is not assignable to type 'string'. |
| 3 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(74,13): error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 4 | + Type 'number' is not assignable to type 'string'. |
| 5 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(91,13): error TS2339: Property 'foo' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
2 | 6 | Property 'foo' does not exist on type '{ kind: "bar"; bar: number; }'.
|
3 |
| -tests/cases/conformance/controlFlow/controlFlowAliasing.ts(64,13): error TS2339: Property 'bar' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
| 7 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(94,13): error TS2339: Property 'bar' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
4 | 8 | Property 'bar' does not exist on type '{ kind: "foo"; foo: string; }'.
|
5 |
| -tests/cases/conformance/controlFlow/controlFlowAliasing.ts(71,13): error TS2339: Property 'foo' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
| 9 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(101,13): error TS2339: Property 'foo' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
6 | 10 | Property 'foo' does not exist on type '{ kind: "bar"; bar: number; }'.
|
7 |
| -tests/cases/conformance/controlFlow/controlFlowAliasing.ts(74,13): error TS2339: Property 'bar' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
| 11 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(104,13): error TS2339: Property 'bar' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
8 | 12 | Property 'bar' does not exist on type '{ kind: "foo"; foo: string; }'.
|
9 |
| -tests/cases/conformance/controlFlow/controlFlowAliasing.ts(82,13): error TS2339: Property 'foo' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
| 13 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(112,13): error TS2339: Property 'foo' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
10 | 14 | Property 'foo' does not exist on type '{ kind: "bar"; bar: number; }'.
|
11 |
| -tests/cases/conformance/controlFlow/controlFlowAliasing.ts(85,13): error TS2339: Property 'bar' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
| 15 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(115,13): error TS2339: Property 'bar' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
12 | 16 | Property 'bar' does not exist on type '{ kind: "foo"; foo: string; }'.
|
13 |
| -tests/cases/conformance/controlFlow/controlFlowAliasing.ts(104,13): error TS2339: Property 'foo' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
| 17 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(134,13): error TS2339: Property 'foo' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
14 | 18 | Property 'foo' does not exist on type '{ kind: "bar"; bar: number; }'.
|
15 |
| -tests/cases/conformance/controlFlow/controlFlowAliasing.ts(107,13): error TS2339: Property 'bar' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
| 19 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(137,13): error TS2339: Property 'bar' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
16 | 20 | Property 'bar' does not exist on type '{ kind: "foo"; foo: string; }'.
|
17 |
| -tests/cases/conformance/controlFlow/controlFlowAliasing.ts(124,19): error TS2339: Property 'foo' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
| 21 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(154,19): error TS2339: Property 'foo' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
18 | 22 | Property 'foo' does not exist on type '{ kind: "bar"; bar: number; }'.
|
19 |
| -tests/cases/conformance/controlFlow/controlFlowAliasing.ts(127,19): error TS2339: Property 'bar' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
| 23 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(157,19): error TS2339: Property 'bar' does not exist on type '{ kind: "foo"; foo: string; } | { kind: "bar"; bar: number; }'. |
20 | 24 | Property 'bar' does not exist on type '{ kind: "foo"; foo: string; }'.
|
21 |
| -tests/cases/conformance/controlFlow/controlFlowAliasing.ts(207,13): error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 25 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(219,13): error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 26 | + Type 'number' is not assignable to type 'string'. |
| 27 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(232,13): error TS2322: Type 'string | number' is not assignable to type 'string'. |
22 | 28 | Type 'number' is not assignable to type 'string'.
|
23 |
| -tests/cases/conformance/controlFlow/controlFlowAliasing.ts(210,13): error TS2322: Type 'string | number' is not assignable to type 'number'. |
| 29 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(233,13): error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 30 | + Type 'number' is not assignable to type 'string'. |
| 31 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(267,13): error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 32 | + Type 'number' is not assignable to type 'string'. |
| 33 | +tests/cases/conformance/controlFlow/controlFlowAliasing.ts(270,13): error TS2322: Type 'string | number' is not assignable to type 'number'. |
24 | 34 | Type 'string' is not assignable to type 'number'.
|
25 | 35 |
|
26 | 36 |
|
27 |
| -==== tests/cases/conformance/controlFlow/controlFlowAliasing.ts (12 errors) ==== |
| 37 | +==== tests/cases/conformance/controlFlow/controlFlowAliasing.ts (17 errors) ==== |
28 | 38 | // Narrowing by aliased conditional expressions
|
29 | 39 |
|
30 | 40 | function f10(x: string | number) {
|
@@ -72,6 +82,42 @@ tests/cases/conformance/controlFlow/controlFlowAliasing.ts(210,13): error TS2322
|
72 | 82 | return notUndefined ? x : 0;
|
73 | 83 | }
|
74 | 84 |
|
| 85 | + function f15(obj: { readonly x: string | number }) { |
| 86 | + const isString = typeof obj.x === 'string'; |
| 87 | + if (isString) { |
| 88 | + let s: string = obj.x; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + function f16(obj: { readonly x: string | number }) { |
| 93 | + const isString = typeof obj.x === 'string'; |
| 94 | + obj = { x: 42 }; |
| 95 | + if (isString) { |
| 96 | + let s: string = obj.x; // Not narrowed because of is assigned in function body |
| 97 | + ~ |
| 98 | +!!! error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 99 | +!!! error TS2322: Type 'number' is not assignable to type 'string'. |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + function f17(obj: readonly [string | number]) { |
| 104 | + const isString = typeof obj[0] === 'string'; |
| 105 | + if (isString) { |
| 106 | + let s: string = obj[0]; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + function f18(obj: readonly [string | number]) { |
| 111 | + const isString = typeof obj[0] === 'string'; |
| 112 | + obj = [42]; |
| 113 | + if (isString) { |
| 114 | + let s: string = obj[0]; // Not narrowed because of is assigned in function body |
| 115 | + ~ |
| 116 | +!!! error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 117 | +!!! error TS2322: Type 'number' is not assignable to type 'string'. |
| 118 | + } |
| 119 | + } |
| 120 | + |
75 | 121 | function f20(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) {
|
76 | 122 | const isFoo = obj.kind === 'foo';
|
77 | 123 | if (isFoo) {
|
@@ -236,6 +282,45 @@ tests/cases/conformance/controlFlow/controlFlowAliasing.ts(210,13): error TS2322
|
236 | 282 | }
|
237 | 283 | }
|
238 | 284 |
|
| 285 | + |
| 286 | + class C10 { |
| 287 | + constructor(readonly x: string | number) { |
| 288 | + const thisX_isString = typeof this.x === 'string'; |
| 289 | + const xIsString = typeof x === 'string'; |
| 290 | + if (thisX_isString && xIsString) { |
| 291 | + let s: string; |
| 292 | + s = this.x; |
| 293 | + ~ |
| 294 | +!!! error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 295 | +!!! error TS2322: Type 'number' is not assignable to type 'string'. |
| 296 | + s = x; |
| 297 | + } |
| 298 | + } |
| 299 | + } |
| 300 | + |
| 301 | + class C11 { |
| 302 | + constructor(readonly x: string | number) { |
| 303 | + const thisX_isString = typeof this.x === 'string'; |
| 304 | + const xIsString = typeof x === 'string'; |
| 305 | + if (thisX_isString && xIsString) { |
| 306 | + // Some narrowings may be invalidated due to later assignments. |
| 307 | + let s: string; |
| 308 | + s = this.x; |
| 309 | + ~ |
| 310 | +!!! error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 311 | +!!! error TS2322: Type 'number' is not assignable to type 'string'. |
| 312 | + s = x; |
| 313 | + ~ |
| 314 | +!!! error TS2322: Type 'string | number' is not assignable to type 'string'. |
| 315 | +!!! error TS2322: Type 'number' is not assignable to type 'string'. |
| 316 | + } |
| 317 | + else { |
| 318 | + this.x = 10; |
| 319 | + x = 10; |
| 320 | + } |
| 321 | + } |
| 322 | + } |
| 323 | + |
239 | 324 | // Mixing of aliased discriminants and conditionals
|
240 | 325 |
|
241 | 326 | function f40(obj: { kind: 'foo', foo?: string } | { kind: 'bar', bar?: number }) {
|
|
0 commit comments