Skip to content

Commit 1731ad3

Browse files
committed
More aggressive parens to match existing DT tests
1 parent 1c6f587 commit 1731ad3

27 files changed

+105
-104
lines changed

Diff for: src/compiler/factory/parenthesizerRules.ts

+1
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,7 @@ namespace ts {
434434
function parenthesizeConstituentTypeOfUnionType(type: TypeNode) {
435435
switch (type.kind) {
436436
case SyntaxKind.UnionType: // Not strictly necessary, but a union containing a union should have been flattened
437+
case SyntaxKind.IntersectionType: // Not strictly necessary, but makes generated output more readable and avoids breaks in DT tests
437438
return factory.createParenthesizedType(type);
438439
}
439440
return parenthesizeCheckTypeOfConditionalType(type);

Diff for: tests/baselines/reference/controlFlowIfStatement.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function c<T>(data: string | T): T {
104104
>JSON.parse : (text: string, reviver?: (this: any, key: string, value: any) => any) => any
105105
>JSON : JSON
106106
>parse : (text: string, reviver?: (this: any, key: string, value: any) => any) => any
107-
>data : string | T & string
107+
>data : string | (T & string)
108108
}
109109
else {
110110
return data;

Diff for: tests/baselines/reference/discriminatedUnionTypes2.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -388,14 +388,14 @@ function foo1(x: RuntimeValue & { type: 'number' }) {
388388

389389
function foo2(x: RuntimeValue & ({ type: 'number' } | { type: 'string' })) {
390390
>foo2 : (x: RuntimeValue & ({ type: 'number';} | { type: 'string';})) => void
391-
>x : { type: "number"; value: number; } & { type: 'number'; } | { type: "string"; value: string; } & { type: 'string'; }
391+
>x : ({ type: "number"; value: number; } & { type: 'number'; }) | ({ type: "string"; value: string; } & { type: 'string'; })
392392
>type : "number"
393393
>type : "string"
394394

395395
if (x.type === 'number') {
396396
>x.type === 'number' : boolean
397397
>x.type : "string" | "number"
398-
>x : { type: "number"; value: number; } & { type: "number"; } | { type: "string"; value: string; } & { type: "string"; }
398+
>x : ({ type: "number"; value: number; } & { type: "number"; }) | ({ type: "string"; value: string; } & { type: "string"; })
399399
>type : "string" | "number"
400400
>'number' : "number"
401401

Diff for: tests/baselines/reference/intersectionAndUnionTypes.errors.txt

+16-16
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(19,1): e
22
Property 'b' is missing in type 'A' but required in type 'B'.
33
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(20,1): error TS2322: Type 'B' is not assignable to type 'A & B'.
44
Property 'a' is missing in type 'B' but required in type 'A'.
5-
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(23,1): error TS2322: Type 'A | B' is not assignable to type 'A & B | C & D'.
6-
Type 'A' is not assignable to type 'A & B | C & D'.
5+
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(23,1): error TS2322: Type 'A | B' is not assignable to type '(A & B) | (C & D)'.
6+
Type 'A' is not assignable to type '(A & B) | (C & D)'.
77
Type 'A' is not assignable to type 'A & B'.
8-
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(25,1): error TS2322: Type 'C | D' is not assignable to type 'A & B | C & D'.
9-
Type 'C' is not assignable to type 'A & B | C & D'.
8+
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(25,1): error TS2322: Type 'C | D' is not assignable to type '(A & B) | (C & D)'.
9+
Type 'C' is not assignable to type '(A & B) | (C & D)'.
1010
Type 'C' is not assignable to type 'C & D'.
1111
Property 'd' is missing in type 'C' but required in type 'D'.
12-
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(26,1): error TS2322: Type 'A & B | C & D' is not assignable to type 'A & B'.
12+
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(26,1): error TS2322: Type '(A & B) | (C & D)' is not assignable to type 'A & B'.
1313
Type 'C & D' is not assignable to type 'A & B'.
1414
Property 'a' is missing in type 'C & D' but required in type 'A'.
15-
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(27,1): error TS2322: Type 'A & B | C & D' is not assignable to type 'A | B'.
15+
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(27,1): error TS2322: Type '(A & B) | (C & D)' is not assignable to type 'A | B'.
1616
Type 'C & D' is not assignable to type 'A | B'.
17-
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(28,1): error TS2322: Type 'A & B | C & D' is not assignable to type 'C & D'.
17+
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(28,1): error TS2322: Type '(A & B) | (C & D)' is not assignable to type 'C & D'.
1818
Type 'A & B' is not assignable to type 'C & D'.
1919
Property 'c' is missing in type 'A & B' but required in type 'C'.
20-
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(29,1): error TS2322: Type 'A & B | C & D' is not assignable to type 'C | D'.
20+
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(29,1): error TS2322: Type '(A & B) | (C & D)' is not assignable to type 'C | D'.
2121
Type 'A & B' is not assignable to type 'C | D'.
2222
tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(31,1): error TS2322: Type 'A & B' is not assignable to type '(A | B) & (C | D)'.
2323
Type 'A & B' is not assignable to type 'B & D'.
@@ -74,36 +74,36 @@ tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts(37,1): e
7474
x = anb; // Ok
7575
x = aob;
7676
~
77-
!!! error TS2322: Type 'A | B' is not assignable to type 'A & B | C & D'.
78-
!!! error TS2322: Type 'A' is not assignable to type 'A & B | C & D'.
77+
!!! error TS2322: Type 'A | B' is not assignable to type '(A & B) | (C & D)'.
78+
!!! error TS2322: Type 'A' is not assignable to type '(A & B) | (C & D)'.
7979
!!! error TS2322: Type 'A' is not assignable to type 'A & B'.
8080
x = cnd; // Ok
8181
x = cod;
8282
~
83-
!!! error TS2322: Type 'C | D' is not assignable to type 'A & B | C & D'.
84-
!!! error TS2322: Type 'C' is not assignable to type 'A & B | C & D'.
83+
!!! error TS2322: Type 'C | D' is not assignable to type '(A & B) | (C & D)'.
84+
!!! error TS2322: Type 'C' is not assignable to type '(A & B) | (C & D)'.
8585
!!! error TS2322: Type 'C' is not assignable to type 'C & D'.
8686
!!! error TS2322: Property 'd' is missing in type 'C' but required in type 'D'.
8787
!!! related TS2728 tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts:4:15: 'd' is declared here.
8888
anb = x;
8989
~~~
90-
!!! error TS2322: Type 'A & B | C & D' is not assignable to type 'A & B'.
90+
!!! error TS2322: Type '(A & B) | (C & D)' is not assignable to type 'A & B'.
9191
!!! error TS2322: Type 'C & D' is not assignable to type 'A & B'.
9292
!!! error TS2322: Property 'a' is missing in type 'C & D' but required in type 'A'.
9393
!!! related TS2728 tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts:1:15: 'a' is declared here.
9494
aob = x;
9595
~~~
96-
!!! error TS2322: Type 'A & B | C & D' is not assignable to type 'A | B'.
96+
!!! error TS2322: Type '(A & B) | (C & D)' is not assignable to type 'A | B'.
9797
!!! error TS2322: Type 'C & D' is not assignable to type 'A | B'.
9898
cnd = x;
9999
~~~
100-
!!! error TS2322: Type 'A & B | C & D' is not assignable to type 'C & D'.
100+
!!! error TS2322: Type '(A & B) | (C & D)' is not assignable to type 'C & D'.
101101
!!! error TS2322: Type 'A & B' is not assignable to type 'C & D'.
102102
!!! error TS2322: Property 'c' is missing in type 'A & B' but required in type 'C'.
103103
!!! related TS2728 tests/cases/conformance/types/intersection/intersectionAndUnionTypes.ts:3:15: 'c' is declared here.
104104
cod = x;
105105
~~~
106-
!!! error TS2322: Type 'A & B | C & D' is not assignable to type 'C | D'.
106+
!!! error TS2322: Type '(A & B) | (C & D)' is not assignable to type 'C | D'.
107107
!!! error TS2322: Type 'A & B' is not assignable to type 'C | D'.
108108

109109
y = anb;

Diff for: tests/baselines/reference/intersectionAndUnionTypes.types

+13-13
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ var cod: C | D;
3636
>cod : C | D
3737

3838
var x: A & B | C & D;
39-
>x : A & B | C & D
39+
>x : (A & B) | (C & D)
4040

4141
var y: (A | B) & (C | D);
4242
>y : (A | B) & (C | D)
@@ -63,43 +63,43 @@ anb = b;
6363

6464
x = anb; // Ok
6565
>x = anb : A & B
66-
>x : A & B | C & D
66+
>x : (A & B) | (C & D)
6767
>anb : A & B
6868

6969
x = aob;
7070
>x = aob : A | B
71-
>x : A & B | C & D
71+
>x : (A & B) | (C & D)
7272
>aob : A | B
7373

7474
x = cnd; // Ok
7575
>x = cnd : C & D
76-
>x : A & B | C & D
76+
>x : (A & B) | (C & D)
7777
>cnd : C & D
7878

7979
x = cod;
8080
>x = cod : C | D
81-
>x : A & B | C & D
81+
>x : (A & B) | (C & D)
8282
>cod : C | D
8383

8484
anb = x;
85-
>anb = x : A & B | C & D
85+
>anb = x : (A & B) | (C & D)
8686
>anb : A & B
87-
>x : A & B | C & D
87+
>x : (A & B) | (C & D)
8888

8989
aob = x;
90-
>aob = x : A & B | C & D
90+
>aob = x : (A & B) | (C & D)
9191
>aob : A | B
92-
>x : A & B | C & D
92+
>x : (A & B) | (C & D)
9393

9494
cnd = x;
95-
>cnd = x : A & B | C & D
95+
>cnd = x : (A & B) | (C & D)
9696
>cnd : C & D
97-
>x : A & B | C & D
97+
>x : (A & B) | (C & D)
9898

9999
cod = x;
100-
>cod = x : A & B | C & D
100+
>cod = x : (A & B) | (C & D)
101101
>cod : C | D
102-
>x : A & B | C & D
102+
>x : (A & B) | (C & D)
103103

104104
y = anb;
105105
>y = anb : A & B

Diff for: tests/baselines/reference/intersectionNarrowing.types

+12-12
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,24 @@
22
// Repros from #43130
33

44
function f1<T>(x: T & string | T & undefined) {
5-
>f1 : <T>(x: T & string | T & undefined) => void
6-
>x : T & string | T & undefined
5+
>f1 : <T>(x: (T & string) | (T & undefined)) => void
6+
>x : (T & string) | (T & undefined)
77

88
if (x) {
9-
>x : T & string | T & undefined
9+
>x : (T & string) | (T & undefined)
1010

1111
x; // Should narrow to T & string
1212
>x : T & string
1313
}
1414
}
1515

1616
function f2<T>(x: T & string | T & undefined) {
17-
>f2 : <T>(x: T & string | T & undefined) => void
18-
>x : T & string | T & undefined
17+
>f2 : <T>(x: (T & string) | (T & undefined)) => void
18+
>x : (T & string) | (T & undefined)
1919

2020
if (x !== undefined) {
2121
>x !== undefined : boolean
22-
>x : T & string | T & undefined
22+
>x : (T & string) | (T & undefined)
2323
>undefined : undefined
2424

2525
x; // Should narrow to T & string
@@ -32,13 +32,13 @@ function f2<T>(x: T & string | T & undefined) {
3232
}
3333

3434
function f3<T>(x: T & string | T & number) {
35-
>f3 : <T>(x: T & string | T & number) => void
36-
>x : T & string | T & number
35+
>f3 : <T>(x: (T & string) | (T & number)) => void
36+
>x : (T & string) | (T & number)
3737

3838
if (typeof x === "string") {
3939
>typeof x === "string" : boolean
4040
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
41-
>x : T & string | T & number
41+
>x : (T & string) | (T & number)
4242
>"string" : "string"
4343

4444
x; // Should narrow to T & string
@@ -51,11 +51,11 @@ function f3<T>(x: T & string | T & number) {
5151
}
5252

5353
function f4<T>(x: T & 1 | T & 2) {
54-
>f4 : <T>(x: T & 1 | T & 2) => void
55-
>x : T & 1 | T & 2
54+
>f4 : <T>(x: (T & 1) | (T & 2)) => void
55+
>x : (T & 1) | (T & 2)
5656

5757
switch (x) {
58-
>x : T & 1 | T & 2
58+
>x : (T & 1) | (T & 2)
5959

6060
case 1: x; break; // T & 1
6161
>1 : 1

Diff for: tests/baselines/reference/intersectionOfUnionNarrowing.types

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ declare const q: X & AorB;
2020

2121
if (q.a !== undefined) {
2222
>q.a !== undefined : boolean
23-
>q.a : { aProp: string; } & object | undefined
23+
>q.a : ({ aProp: string; } & object) | undefined
2424
>q : X & AorB
25-
>a : { aProp: string; } & object | undefined
25+
>a : ({ aProp: string; } & object) | undefined
2626
>undefined : undefined
2727

2828
q.a.aProp;

Diff for: tests/baselines/reference/keyofAndIndexedAccess2.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ for (const action of actions) {
564564

565565
window[action](x, y);
566566
>window[action](x, y) : void
567-
>window[action] : ((x: number, y: number) => void) & ((x: number, y: number) => void) | ((width: number, height: number) => void) & ((width: number, height: number) => void)
567+
>window[action] : (((x: number, y: number) => void) & ((x: number, y: number) => void)) | (((width: number, height: number) => void) & ((width: number, height: number) => void))
568568
>window : Window & typeof globalThis
569569
>action : "resizeTo" | "resizeBy"
570570
>x : number

Diff for: tests/baselines/reference/narrowingByTypeofInSwitch.types

+4-4
Original file line numberDiff line numberDiff line change
@@ -564,8 +564,8 @@ function multipleGenericFuse<X extends L | number, Y extends R | number>(xy: X |
564564

565565
case 'number': return [xy]
566566
>'number' : "number"
567-
>[xy] : [X & number | Y & number]
568-
>xy : X & number | Y & number
567+
>[xy] : [(X & number) | (Y & number)]
568+
>xy : (X & number) | (Y & number)
569569
}
570570
}
571571

@@ -1104,8 +1104,8 @@ function multipleGenericFuseWithBoth<X extends L | number, Y extends R | number>
11041104

11051105
case `number`: return [xy]
11061106
>`number` : "number"
1107-
>[xy] : [X & number | Y & number]
1108-
>xy : X & number | Y & number
1107+
>[xy] : [(X & number) | (Y & number)]
1108+
>xy : (X & number) | (Y & number)
11091109
}
11101110
}
11111111

Diff for: tests/baselines/reference/narrowingTypeofFunction.types

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ interface F { (): string }
77

88
function f1(a: (F & Meta) | string) {
99
>f1 : (a: (F & Meta) | string) => void
10-
>a : string | F & Meta
10+
>a : string | (F & Meta)
1111

1212
if (typeof a === "function") {
1313
>typeof a === "function" : boolean
1414
>typeof a : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
15-
>a : string | F & Meta
15+
>a : string | (F & Meta)
1616
>"function" : "function"
1717

1818
a;
@@ -25,17 +25,17 @@ function f1(a: (F & Meta) | string) {
2525
}
2626

2727
function f2<T>(x: (T & F) | T & string) {
28-
>f2 : <T>(x: (T & F) | T & string) => void
29-
>x : T & F | T & string
28+
>f2 : <T>(x: (T & F) | (T & string)) => void
29+
>x : (T & F) | (T & string)
3030

3131
if (typeof x === "function") {
3232
>typeof x === "function" : boolean
3333
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
34-
>x : T & F | T & string
34+
>x : (T & F) | (T & string)
3535
>"function" : "function"
3636

3737
x;
38-
>x : T & F | T & string
38+
>x : (T & F) | (T & string)
3939
}
4040
else {
4141
x;

Diff for: tests/baselines/reference/objectLiteralExcessProperties.errors.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tests/cases/compiler/objectLiteralExcessProperties.ts(41,11): error TS2322: Type
2626
'{ name: string; prop: boolean; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'IFoo'.
2727
tests/cases/compiler/objectLiteralExcessProperties.ts(43,43): error TS2322: Type '{ name: string; prop: true; }' is not assignable to type 'T | { prop: boolean; }'.
2828
Object literal may only specify known properties, and 'name' does not exist in type '{ prop: boolean; }'.
29-
tests/cases/compiler/objectLiteralExcessProperties.ts(45,76): error TS2322: Type '{ name: string; prop: boolean; }' is not assignable to type 'T & { prop: boolean; } | { name: string; }'.
29+
tests/cases/compiler/objectLiteralExcessProperties.ts(45,76): error TS2322: Type '{ name: string; prop: boolean; }' is not assignable to type '(T & { prop: boolean; }) | { name: string; }'.
3030
Object literal may only specify known properties, and 'prop' does not exist in type '{ name: string; }'.
3131
tests/cases/compiler/objectLiteralExcessProperties.ts(49,44): error TS2322: Type '{ z: string; }' is not assignable to type 'object & { x: string; }'.
3232
Object literal may only specify known properties, and 'z' does not exist in type 'object & { x: string; }'.
@@ -122,7 +122,7 @@ tests/cases/compiler/objectLiteralExcessProperties.ts(49,44): error TS2322: Type
122122
// Excess property checks only on non-generic parts of unions
123123
const obj4: T & { prop: boolean } | { name: string } = { name: "test", prop: true };
124124
~~~~~~~~~~
125-
!!! error TS2322: Type '{ name: string; prop: boolean; }' is not assignable to type 'T & { prop: boolean; } | { name: string; }'.
125+
!!! error TS2322: Type '{ name: string; prop: boolean; }' is not assignable to type '(T & { prop: boolean; }) | { name: string; }'.
126126
!!! error TS2322: Object literal may only specify known properties, and 'prop' does not exist in type '{ name: string; }'.
127127
// No excess property checks when union includes 'object' type
128128
const obj5: object | { x: string } = { z: 'abc' }

Diff for: tests/baselines/reference/objectLiteralExcessProperties.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function test<T extends IFoo>() {
132132

133133
// Excess property checks only on non-generic parts of unions
134134
const obj4: T & { prop: boolean } | { name: string } = { name: "test", prop: true };
135-
>obj4 : T & { prop: boolean; } | { name: string; }
135+
>obj4 : (T & { prop: boolean; }) | { name: string; }
136136
>prop : boolean
137137
>name : string
138138
>{ name: "test", prop: true } : { name: string; prop: boolean; }

Diff for: tests/baselines/reference/objectSpread.types

+12-12
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,8 @@ function genericSpread<T, U>(t: T, u: U, v: T | U, w: T | { s: string }, obj: {
670670
>v : T | U
671671

672672
let x12 = { ...v, ...obj };
673-
>x12 : T & { x: number; } | U & { x: number; }
674-
>{ ...v, ...obj } : T & { x: number; } | U & { x: number; }
673+
>x12 : (T & { x: number; }) | (U & { x: number; })
674+
>{ ...v, ...obj } : (T & { x: number; }) | (U & { x: number; })
675675
>v : T | U
676676
>obj : { x: number; }
677677

@@ -681,33 +681,33 @@ function genericSpread<T, U>(t: T, u: U, v: T | U, w: T | { s: string }, obj: {
681681
>w : T | { s: string; }
682682

683683
let x14 = { ...w, ...obj };
684-
>x14 : T & { x: number; } | { x: number; s: string; }
685-
>{ ...w, ...obj } : T & { x: number; } | { x: number; s: string; }
684+
>x14 : (T & { x: number; }) | { x: number; s: string; }
685+
>{ ...w, ...obj } : (T & { x: number; }) | { x: number; s: string; }
686686
>w : T | { s: string; }
687687
>obj : { x: number; }
688688

689689
let x15 = { ...t, ...v };
690-
>x15 : T | T & U
691-
>{ ...t, ...v } : T | T & U
690+
>x15 : T | (T & U)
691+
>{ ...t, ...v } : T | (T & U)
692692
>t : T
693693
>v : T | U
694694

695695
let x16 = { ...t, ...w };
696-
>x16 : T | T & { s: string; }
697-
>{ ...t, ...w } : T | T & { s: string; }
696+
>x16 : T | (T & { s: string; })
697+
>{ ...t, ...w } : T | (T & { s: string; })
698698
>t : T
699699
>w : T | { s: string; }
700700

701701
let x17 = { ...t, ...w, ...obj };
702-
>x17 : T & { x: number; } | T & { x: number; s: string; }
703-
>{ ...t, ...w, ...obj } : T & { x: number; } | T & { x: number; s: string; }
702+
>x17 : (T & { x: number; }) | (T & { x: number; s: string; })
703+
>{ ...t, ...w, ...obj } : (T & { x: number; }) | (T & { x: number; s: string; })
704704
>t : T
705705
>w : T | { s: string; }
706706
>obj : { x: number; }
707707

708708
let x18 = { ...t, ...v, ...w };
709-
>x18 : T | T & U | T & { s: string; } | T & U & { s: string; }
710-
>{ ...t, ...v, ...w } : T | T & U | T & { s: string; } | T & U & { s: string; }
709+
>x18 : T | (T & U) | (T & { s: string; }) | (T & U & { s: string; })
710+
>{ ...t, ...v, ...w } : T | (T & U) | (T & { s: string; }) | (T & U & { s: string; })
711711
>t : T
712712
>v : T | U
713713
>w : T | { s: string; }

0 commit comments

Comments
 (0)