Skip to content

Commit 3b12c04

Browse files
Accepted baselines.
1 parent 6f86803 commit 3b12c04

File tree

4 files changed

+351
-49
lines changed

4 files changed

+351
-49
lines changed

tests/baselines/reference/controlFlowAliasing.errors.txt

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,19 @@ tests/cases/conformance/controlFlow/controlFlowAliasing.ts(154,19): error TS2339
2222
Property 'foo' does not exist on type '{ kind: "bar"; bar: number; }'.
2323
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; }'.
2424
Property 'bar' does not exist on type '{ kind: "foo"; foo: string; }'.
25-
tests/cases/conformance/controlFlow/controlFlowAliasing.ts(237,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'.
2626
Type 'number' is not assignable to type 'string'.
27-
tests/cases/conformance/controlFlow/controlFlowAliasing.ts(240,13): error TS2322: Type 'string | number' is not assignable to type 'number'.
27+
tests/cases/conformance/controlFlow/controlFlowAliasing.ts(232,13): error TS2322: Type 'string | number' is not assignable to type 'string'.
28+
Type 'number' is not assignable to type 'string'.
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'.
2834
Type 'string' is not assignable to type 'number'.
2935

3036

31-
==== tests/cases/conformance/controlFlow/controlFlowAliasing.ts (14 errors) ====
37+
==== tests/cases/conformance/controlFlow/controlFlowAliasing.ts (17 errors) ====
3238
// Narrowing by aliased conditional expressions
3339

3440
function f10(x: string | number) {
@@ -276,6 +282,45 @@ tests/cases/conformance/controlFlow/controlFlowAliasing.ts(240,13): error TS2322
276282
}
277283
}
278284

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+
279324
// Mixing of aliased discriminants and conditionals
280325

281326
function f40(obj: { kind: 'foo', foo?: string } | { kind: 'bar', bar?: number }) {

tests/baselines/reference/controlFlowAliasing.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,36 @@ function f33(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) {
210210
}
211211
}
212212

213+
214+
class C10 {
215+
constructor(readonly x: string | number) {
216+
const thisX_isString = typeof this.x === 'string';
217+
const xIsString = typeof x === 'string';
218+
if (thisX_isString && xIsString) {
219+
let s: string;
220+
s = this.x;
221+
s = x;
222+
}
223+
}
224+
}
225+
226+
class C11 {
227+
constructor(readonly x: string | number) {
228+
const thisX_isString = typeof this.x === 'string';
229+
const xIsString = typeof x === 'string';
230+
if (thisX_isString && xIsString) {
231+
// Some narrowings may be invalidated due to later assignments.
232+
let s: string;
233+
s = this.x;
234+
s = x;
235+
}
236+
else {
237+
this.x = 10;
238+
x = 10;
239+
}
240+
}
241+
}
242+
213243
// Mixing of aliased discriminants and conditionals
214244

215245
function f40(obj: { kind: 'foo', foo?: string } | { kind: 'bar', bar?: number }) {
@@ -436,6 +466,37 @@ function f33(obj) {
436466
break;
437467
}
438468
}
469+
var C10 = /** @class */ (function () {
470+
function C10(x) {
471+
this.x = x;
472+
var thisX_isString = typeof this.x === 'string';
473+
var xIsString = typeof x === 'string';
474+
if (thisX_isString && xIsString) {
475+
var s = void 0;
476+
s = this.x;
477+
s = x;
478+
}
479+
}
480+
return C10;
481+
}());
482+
var C11 = /** @class */ (function () {
483+
function C11(x) {
484+
this.x = x;
485+
var thisX_isString = typeof this.x === 'string';
486+
var xIsString = typeof x === 'string';
487+
if (thisX_isString && xIsString) {
488+
// Some narrowings may be invalidated due to later assignments.
489+
var s = void 0;
490+
s = this.x;
491+
s = x;
492+
}
493+
else {
494+
this.x = 10;
495+
x = 10;
496+
}
497+
}
498+
return C11;
499+
}());
439500
// Mixing of aliased discriminants and conditionals
440501
function f40(obj) {
441502
var kind = obj.kind;
@@ -572,6 +633,14 @@ declare function f33(obj: {
572633
kind: 'bar';
573634
bar: number;
574635
}): void;
636+
declare class C10 {
637+
readonly x: string | number;
638+
constructor(x: string | number);
639+
}
640+
declare class C11 {
641+
readonly x: string | number;
642+
constructor(x: string | number);
643+
}
575644
declare function f40(obj: {
576645
kind: 'foo';
577646
foo?: string;

tests/baselines/reference/controlFlowAliasing.symbols

Lines changed: 129 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -603,89 +603,172 @@ function f33(obj: { kind: 'foo', foo: string } | { kind: 'bar', bar: number }) {
603603
}
604604
}
605605

606+
607+
class C10 {
608+
>C10 : Symbol(C10, Decl(controlFlowAliasing.ts, 209, 1))
609+
610+
constructor(readonly x: string | number) {
611+
>x : Symbol(C10.x, Decl(controlFlowAliasing.ts, 213, 16))
612+
613+
const thisX_isString = typeof this.x === 'string';
614+
>thisX_isString : Symbol(thisX_isString, Decl(controlFlowAliasing.ts, 214, 13))
615+
>this.x : Symbol(C10.x, Decl(controlFlowAliasing.ts, 213, 16))
616+
>this : Symbol(C10, Decl(controlFlowAliasing.ts, 209, 1))
617+
>x : Symbol(C10.x, Decl(controlFlowAliasing.ts, 213, 16))
618+
619+
const xIsString = typeof x === 'string';
620+
>xIsString : Symbol(xIsString, Decl(controlFlowAliasing.ts, 215, 13))
621+
>x : Symbol(x, Decl(controlFlowAliasing.ts, 213, 16))
622+
623+
if (thisX_isString && xIsString) {
624+
>thisX_isString : Symbol(thisX_isString, Decl(controlFlowAliasing.ts, 214, 13))
625+
>xIsString : Symbol(xIsString, Decl(controlFlowAliasing.ts, 215, 13))
626+
627+
let s: string;
628+
>s : Symbol(s, Decl(controlFlowAliasing.ts, 217, 15))
629+
630+
s = this.x;
631+
>s : Symbol(s, Decl(controlFlowAliasing.ts, 217, 15))
632+
>this.x : Symbol(C10.x, Decl(controlFlowAliasing.ts, 213, 16))
633+
>this : Symbol(C10, Decl(controlFlowAliasing.ts, 209, 1))
634+
>x : Symbol(C10.x, Decl(controlFlowAliasing.ts, 213, 16))
635+
636+
s = x;
637+
>s : Symbol(s, Decl(controlFlowAliasing.ts, 217, 15))
638+
>x : Symbol(x, Decl(controlFlowAliasing.ts, 213, 16))
639+
}
640+
}
641+
}
642+
643+
class C11 {
644+
>C11 : Symbol(C11, Decl(controlFlowAliasing.ts, 222, 1))
645+
646+
constructor(readonly x: string | number) {
647+
>x : Symbol(C11.x, Decl(controlFlowAliasing.ts, 225, 16))
648+
649+
const thisX_isString = typeof this.x === 'string';
650+
>thisX_isString : Symbol(thisX_isString, Decl(controlFlowAliasing.ts, 226, 13))
651+
>this.x : Symbol(C11.x, Decl(controlFlowAliasing.ts, 225, 16))
652+
>this : Symbol(C11, Decl(controlFlowAliasing.ts, 222, 1))
653+
>x : Symbol(C11.x, Decl(controlFlowAliasing.ts, 225, 16))
654+
655+
const xIsString = typeof x === 'string';
656+
>xIsString : Symbol(xIsString, Decl(controlFlowAliasing.ts, 227, 13))
657+
>x : Symbol(x, Decl(controlFlowAliasing.ts, 225, 16))
658+
659+
if (thisX_isString && xIsString) {
660+
>thisX_isString : Symbol(thisX_isString, Decl(controlFlowAliasing.ts, 226, 13))
661+
>xIsString : Symbol(xIsString, Decl(controlFlowAliasing.ts, 227, 13))
662+
663+
// Some narrowings may be invalidated due to later assignments.
664+
let s: string;
665+
>s : Symbol(s, Decl(controlFlowAliasing.ts, 230, 15))
666+
667+
s = this.x;
668+
>s : Symbol(s, Decl(controlFlowAliasing.ts, 230, 15))
669+
>this.x : Symbol(C11.x, Decl(controlFlowAliasing.ts, 225, 16))
670+
>this : Symbol(C11, Decl(controlFlowAliasing.ts, 222, 1))
671+
>x : Symbol(C11.x, Decl(controlFlowAliasing.ts, 225, 16))
672+
673+
s = x;
674+
>s : Symbol(s, Decl(controlFlowAliasing.ts, 230, 15))
675+
>x : Symbol(x, Decl(controlFlowAliasing.ts, 225, 16))
676+
}
677+
else {
678+
this.x = 10;
679+
>this.x : Symbol(C11.x, Decl(controlFlowAliasing.ts, 225, 16))
680+
>this : Symbol(C11, Decl(controlFlowAliasing.ts, 222, 1))
681+
>x : Symbol(C11.x, Decl(controlFlowAliasing.ts, 225, 16))
682+
683+
x = 10;
684+
>x : Symbol(x, Decl(controlFlowAliasing.ts, 225, 16))
685+
}
686+
}
687+
}
688+
606689
// Mixing of aliased discriminants and conditionals
607690

608691
function f40(obj: { kind: 'foo', foo?: string } | { kind: 'bar', bar?: number }) {
609-
>f40 : Symbol(f40, Decl(controlFlowAliasing.ts, 209, 1))
610-
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 213, 13))
611-
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 213, 19))
612-
>foo : Symbol(foo, Decl(controlFlowAliasing.ts, 213, 32))
613-
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 213, 51))
614-
>bar : Symbol(bar, Decl(controlFlowAliasing.ts, 213, 64))
692+
>f40 : Symbol(f40, Decl(controlFlowAliasing.ts, 239, 1))
693+
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 243, 13))
694+
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 243, 19))
695+
>foo : Symbol(foo, Decl(controlFlowAliasing.ts, 243, 32))
696+
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 243, 51))
697+
>bar : Symbol(bar, Decl(controlFlowAliasing.ts, 243, 64))
615698

616699
const { kind } = obj;
617-
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 214, 11))
618-
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 213, 13))
700+
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 244, 11))
701+
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 243, 13))
619702

620703
const isFoo = kind == 'foo';
621-
>isFoo : Symbol(isFoo, Decl(controlFlowAliasing.ts, 215, 9))
622-
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 214, 11))
704+
>isFoo : Symbol(isFoo, Decl(controlFlowAliasing.ts, 245, 9))
705+
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 244, 11))
623706

624707
if (isFoo && obj.foo) {
625-
>isFoo : Symbol(isFoo, Decl(controlFlowAliasing.ts, 215, 9))
626-
>obj.foo : Symbol(foo, Decl(controlFlowAliasing.ts, 213, 32))
627-
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 213, 13))
628-
>foo : Symbol(foo, Decl(controlFlowAliasing.ts, 213, 32))
708+
>isFoo : Symbol(isFoo, Decl(controlFlowAliasing.ts, 245, 9))
709+
>obj.foo : Symbol(foo, Decl(controlFlowAliasing.ts, 243, 32))
710+
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 243, 13))
711+
>foo : Symbol(foo, Decl(controlFlowAliasing.ts, 243, 32))
629712

630713
let t: string = obj.foo;
631-
>t : Symbol(t, Decl(controlFlowAliasing.ts, 217, 11))
632-
>obj.foo : Symbol(foo, Decl(controlFlowAliasing.ts, 213, 32))
633-
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 213, 13))
634-
>foo : Symbol(foo, Decl(controlFlowAliasing.ts, 213, 32))
714+
>t : Symbol(t, Decl(controlFlowAliasing.ts, 247, 11))
715+
>obj.foo : Symbol(foo, Decl(controlFlowAliasing.ts, 243, 32))
716+
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 243, 13))
717+
>foo : Symbol(foo, Decl(controlFlowAliasing.ts, 243, 32))
635718
}
636719
}
637720

638721
// Unsupported narrowing of destructured payload by destructured discriminant
639722

640723
type Data = { kind: 'str', payload: string } | { kind: 'num', payload: number };
641-
>Data : Symbol(Data, Decl(controlFlowAliasing.ts, 219, 1))
642-
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 223, 13))
643-
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 223, 26))
644-
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 223, 48))
645-
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 223, 61))
724+
>Data : Symbol(Data, Decl(controlFlowAliasing.ts, 249, 1))
725+
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 253, 13))
726+
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 253, 26))
727+
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 253, 48))
728+
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 253, 61))
646729

647730
function gg2(obj: Data) {
648-
>gg2 : Symbol(gg2, Decl(controlFlowAliasing.ts, 223, 80))
649-
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 225, 13))
650-
>Data : Symbol(Data, Decl(controlFlowAliasing.ts, 219, 1))
731+
>gg2 : Symbol(gg2, Decl(controlFlowAliasing.ts, 253, 80))
732+
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 255, 13))
733+
>Data : Symbol(Data, Decl(controlFlowAliasing.ts, 249, 1))
651734

652735
if (obj.kind === 'str') {
653-
>obj.kind : Symbol(kind, Decl(controlFlowAliasing.ts, 223, 13), Decl(controlFlowAliasing.ts, 223, 48))
654-
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 225, 13))
655-
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 223, 13), Decl(controlFlowAliasing.ts, 223, 48))
736+
>obj.kind : Symbol(kind, Decl(controlFlowAliasing.ts, 253, 13), Decl(controlFlowAliasing.ts, 253, 48))
737+
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 255, 13))
738+
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 253, 13), Decl(controlFlowAliasing.ts, 253, 48))
656739

657740
let t: string = obj.payload;
658-
>t : Symbol(t, Decl(controlFlowAliasing.ts, 227, 11))
659-
>obj.payload : Symbol(payload, Decl(controlFlowAliasing.ts, 223, 26))
660-
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 225, 13))
661-
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 223, 26))
741+
>t : Symbol(t, Decl(controlFlowAliasing.ts, 257, 11))
742+
>obj.payload : Symbol(payload, Decl(controlFlowAliasing.ts, 253, 26))
743+
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 255, 13))
744+
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 253, 26))
662745
}
663746
else {
664747
let t: number = obj.payload;
665-
>t : Symbol(t, Decl(controlFlowAliasing.ts, 230, 11))
666-
>obj.payload : Symbol(payload, Decl(controlFlowAliasing.ts, 223, 61))
667-
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 225, 13))
668-
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 223, 61))
748+
>t : Symbol(t, Decl(controlFlowAliasing.ts, 260, 11))
749+
>obj.payload : Symbol(payload, Decl(controlFlowAliasing.ts, 253, 61))
750+
>obj : Symbol(obj, Decl(controlFlowAliasing.ts, 255, 13))
751+
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 253, 61))
669752
}
670753
}
671754

672755
function foo({ kind, payload }: Data) {
673-
>foo : Symbol(foo, Decl(controlFlowAliasing.ts, 232, 1))
674-
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 234, 14))
675-
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 234, 20))
676-
>Data : Symbol(Data, Decl(controlFlowAliasing.ts, 219, 1))
756+
>foo : Symbol(foo, Decl(controlFlowAliasing.ts, 262, 1))
757+
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 264, 14))
758+
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 264, 20))
759+
>Data : Symbol(Data, Decl(controlFlowAliasing.ts, 249, 1))
677760

678761
if (kind === 'str') {
679-
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 234, 14))
762+
>kind : Symbol(kind, Decl(controlFlowAliasing.ts, 264, 14))
680763

681764
let t: string = payload;
682-
>t : Symbol(t, Decl(controlFlowAliasing.ts, 236, 11))
683-
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 234, 20))
765+
>t : Symbol(t, Decl(controlFlowAliasing.ts, 266, 11))
766+
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 264, 20))
684767
}
685768
else {
686769
let t: number = payload;
687-
>t : Symbol(t, Decl(controlFlowAliasing.ts, 239, 11))
688-
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 234, 20))
770+
>t : Symbol(t, Decl(controlFlowAliasing.ts, 269, 11))
771+
>payload : Symbol(payload, Decl(controlFlowAliasing.ts, 264, 20))
689772
}
690773
}
691774

0 commit comments

Comments
 (0)