Skip to content

Commit 9fb6cec

Browse files
authored
Print parenthesis around intersections in unions (#1240)
1 parent 96bcae5 commit 9fb6cec

File tree

81 files changed

+224
-1037
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+224
-1037
lines changed

internal/printer/printer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1964,7 +1964,7 @@ func (p *Printer) emitNamedTupleMember(node *ast.NamedTupleMember) {
19641964
}
19651965

19661966
func (p *Printer) emitUnionTypeConstituent(node *ast.TypeNode) {
1967-
p.emitTypeNode(node, ast.TypePrecedenceIntersection)
1967+
p.emitTypeNode(node, ast.TypePrecedenceTypeOperator)
19681968
}
19691969

19701970
func (p *Printer) emitUnionType(node *ast.UnionTypeNode) {

testdata/baselines/reference/submodule/compiler/contextualTypeFunctionObjectPropertyIntersection.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ interface MachineConfig<TEvent extends { type: string }> {
1919

2020
};
2121
on?: {
22-
>on : { [K in TEvent["type"]]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; } | undefined
22+
>on : ({ [K in TEvent["type"]]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; }) | undefined
2323

2424
[K in TEvent["type"]]?: Action<TEvent extends { type: K } ? TEvent : never>;
2525
>type : K
@@ -121,7 +121,7 @@ interface MachineConfig2<TEvent extends { type: string }> {
121121

122122
};
123123
on?: {
124-
>on : { [K in TEvent["type"] as K extends Uppercase<string> ? K : never]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; } | undefined
124+
>on : ({ [K in TEvent["type"] as K extends Uppercase<string> ? K : never]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; }) | undefined
125125

126126
[K in TEvent["type"] as K extends Uppercase<string> ? K : never]?: Action<TEvent extends { type: K } ? TEvent : never>;
127127
>type : K

testdata/baselines/reference/submodule/compiler/contextualTypeFunctionObjectPropertyIntersection.types.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
};
66
on?: {
77
->on : ({ [K in TEvent["type"]]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent>; }) | undefined
8-
+>on : { [K in TEvent["type"]]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; } | undefined
8+
+>on : ({ [K in TEvent["type"]]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; }) | undefined
99

1010
[K in TEvent["type"]]?: Action<TEvent extends { type: K } ? TEvent : never>;
1111
>type : K
@@ -14,7 +14,7 @@
1414
};
1515
on?: {
1616
->on : ({ [K in TEvent["type"] as K extends Uppercase<string> ? K : never]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent>; }) | undefined
17-
+>on : { [K in TEvent["type"] as K extends Uppercase<string> ? K : never]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; } | undefined
17+
+>on : ({ [K in TEvent["type"] as K extends Uppercase<string> ? K : never]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; }) | undefined
1818

1919
[K in TEvent["type"] as K extends Uppercase<string> ? K : never]?: Action<TEvent extends { type: K } ? TEvent : never>;
2020
>type : K

testdata/baselines/reference/submodule/compiler/excessPropertyCheckIntersectionWithRecursiveType.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
excessPropertyCheckIntersectionWithRecursiveType.ts(13,9): error TS2353: Object literal may only specify known properties, and 'invalid' does not exist in type '{ l2: Schema1<boolean>; }'.
2-
excessPropertyCheckIntersectionWithRecursiveType.ts(26,9): error TS2353: Object literal may only specify known properties, and 'invalid' does not exist in type '{ l2: { type: "boolean"; } & Example<false> | { type: "boolean"; } & Example<true>; }'.
2+
excessPropertyCheckIntersectionWithRecursiveType.ts(26,9): error TS2353: Object literal may only specify known properties, and 'invalid' does not exist in type '{ l2: ({ type: "boolean"; } & Example<false>) | ({ type: "boolean"; } & Example<true>); }'.
33
excessPropertyCheckIntersectionWithRecursiveType.ts(39,9): error TS2353: Object literal may only specify known properties, and 'invalid' does not exist in type '{ l2: Schema3<boolean>; }'.
4-
excessPropertyCheckIntersectionWithRecursiveType.ts(52,9): error TS2353: Object literal may only specify known properties, and 'invalid' does not exist in type 'Example<{ l2: boolean; }> & { l2: { type: "boolean"; } & Example<false> | { type: "boolean"; } & Example<true>; }'.
4+
excessPropertyCheckIntersectionWithRecursiveType.ts(52,9): error TS2353: Object literal may only specify known properties, and 'invalid' does not exist in type 'Example<{ l2: boolean; }> & { l2: ({ type: "boolean"; } & Example<false>) | ({ type: "boolean"; } & Example<true>); }'.
55
excessPropertyCheckIntersectionWithRecursiveType.ts(86,11): error TS2353: Object literal may only specify known properties, and 'children' does not exist in type 'User'.
66
excessPropertyCheckIntersectionWithRecursiveType.ts(102,35): error TS2339: Property 'children' does not exist on type 'User'.
77

@@ -37,8 +37,8 @@ excessPropertyCheckIntersectionWithRecursiveType.ts(102,35): error TS2339: Prope
3737
l2: { type: 'boolean' },
3838
invalid: false,
3939
~~~~~~~
40-
!!! error TS2353: Object literal may only specify known properties, and 'invalid' does not exist in type '{ l2: { type: "boolean"; } & Example<false> | { type: "boolean"; } & Example<true>; }'.
41-
!!! related TS6500 excessPropertyCheckIntersectionWithRecursiveType.ts:19:78: The expected type comes from property 'props' which is declared here on type '{ props: { l2: { type: "boolean"; } & Example<false> | { type: "boolean"; } & Example<true>; }; } & Example<{ l2: boolean; }>'
40+
!!! error TS2353: Object literal may only specify known properties, and 'invalid' does not exist in type '{ l2: ({ type: "boolean"; } & Example<false>) | ({ type: "boolean"; } & Example<true>); }'.
41+
!!! related TS6500 excessPropertyCheckIntersectionWithRecursiveType.ts:19:78: The expected type comes from property 'props' which is declared here on type '{ props: { l2: ({ type: "boolean"; } & Example<false>) | ({ type: "boolean"; } & Example<true>); }; } & Example<{ l2: boolean; }>'
4242
},
4343
},
4444
},
@@ -69,8 +69,8 @@ excessPropertyCheckIntersectionWithRecursiveType.ts(102,35): error TS2339: Prope
6969
l2: { type: 'boolean' },
7070
invalid: false,
7171
~~~~~~~
72-
!!! error TS2353: Object literal may only specify known properties, and 'invalid' does not exist in type 'Example<{ l2: boolean; }> & { l2: { type: "boolean"; } & Example<false> | { type: "boolean"; } & Example<true>; }'.
73-
!!! related TS6500 excessPropertyCheckIntersectionWithRecursiveType.ts:45:78: The expected type comes from property 'props' which is declared here on type '{ props: Example<{ l2: boolean; }> & { l2: { type: "boolean"; } & Example<false> | { type: "boolean"; } & Example<true>; }; }'
72+
!!! error TS2353: Object literal may only specify known properties, and 'invalid' does not exist in type 'Example<{ l2: boolean; }> & { l2: ({ type: "boolean"; } & Example<false>) | ({ type: "boolean"; } & Example<true>); }'.
73+
!!! related TS6500 excessPropertyCheckIntersectionWithRecursiveType.ts:45:78: The expected type comes from property 'props' which is declared here on type '{ props: Example<{ l2: boolean; }> & { l2: ({ type: "boolean"; } & Example<false>) | ({ type: "boolean"; } & Example<true>); }; }'
7474
},
7575
},
7676
},

testdata/baselines/reference/submodule/compiler/excessPropertyCheckIntersectionWithRecursiveType.errors.txt.diff

Lines changed: 0 additions & 34 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/excessPropertyCheckIntersectionWithRecursiveType.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type Schema2<T> = (T extends boolean ? { type: 'boolean'; } & Example<T> : { pro
5454
>props : { [P in keyof T]: Schema2<T[P]>; }
5555

5656
export const schemaObj2: Schema2<Request> = {
57-
>schemaObj2 : { props: { l1: { props: { l2: { type: "boolean"; } & Example<false> | { type: "boolean"; } & Example<true>; }; } & Example<{ l2: boolean; }>; }; } & Example<Request>
57+
>schemaObj2 : { props: { l1: { props: { l2: ({ type: "boolean"; } & Example<false>) | ({ type: "boolean"; } & Example<true>); }; } & Example<{ l2: boolean; }>; }; } & Example<Request>
5858
>{ props: { l1: { props: { l2: { type: 'boolean' }, invalid: false, }, }, },} : { props: { l1: { props: { l2: { type: "boolean"; }; invalid: boolean; }; }; }; }
5959

6060
props: {
@@ -126,7 +126,7 @@ type Schema4<T> = (T extends boolean ? { type: 'boolean'; } & Example<T> : { pro
126126
>props : Example<T> & { [P in keyof T]: Schema4<T[P]>; }
127127

128128
export const schemaObj4: Schema4<Request> = {
129-
>schemaObj4 : { props: Example<Request> & { l1: { props: Example<{ l2: boolean; }> & { l2: { type: "boolean"; } & Example<false> | { type: "boolean"; } & Example<true>; }; }; }; }
129+
>schemaObj4 : { props: Example<Request> & { l1: { props: Example<{ l2: boolean; }> & { l2: ({ type: "boolean"; } & Example<false>) | ({ type: "boolean"; } & Example<true>); }; }; }; }
130130
>{ props: { l1: { props: { l2: { type: 'boolean' }, invalid: false, }, }, },} : { props: { l1: { props: { l2: { type: "boolean"; }; invalid: boolean; }; }; }; }
131131

132132
props: {

testdata/baselines/reference/submodule/compiler/excessPropertyCheckIntersectionWithRecursiveType.types.diff

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,6 @@
11
--- old.excessPropertyCheckIntersectionWithRecursiveType.types
22
+++ new.excessPropertyCheckIntersectionWithRecursiveType.types
3-
@@= skipped -53, +53 lines =@@
4-
>props : { [P in keyof T]: Schema2<T[P]>; }
5-
6-
export const schemaObj2: Schema2<Request> = {
7-
->schemaObj2 : { props: { l1: { props: { l2: ({ type: "boolean"; } & Example<false>) | ({ type: "boolean"; } & Example<true>); }; } & Example<{ l2: boolean; }>; }; } & Example<Request>
8-
+>schemaObj2 : { props: { l1: { props: { l2: { type: "boolean"; } & Example<false> | { type: "boolean"; } & Example<true>; }; } & Example<{ l2: boolean; }>; }; } & Example<Request>
9-
>{ props: { l1: { props: { l2: { type: 'boolean' }, invalid: false, }, }, },} : { props: { l1: { props: { l2: { type: "boolean"; }; invalid: boolean; }; }; }; }
10-
11-
props: {
12-
@@= skipped -72, +72 lines =@@
13-
>props : Example<T> & { [P in keyof T]: Schema4<T[P]>; }
14-
15-
export const schemaObj4: Schema4<Request> = {
16-
->schemaObj4 : { props: Example<Request> & { l1: { props: Example<{ l2: boolean; }> & { l2: ({ type: "boolean"; } & Example<false>) | ({ type: "boolean"; } & Example<true>); }; }; }; }
17-
+>schemaObj4 : { props: Example<Request> & { l1: { props: Example<{ l2: boolean; }> & { l2: { type: "boolean"; } & Example<false> | { type: "boolean"; } & Example<true>; }; }; }; }
18-
>{ props: { l1: { props: { l2: { type: 'boolean' }, invalid: false, }, }, },} : { props: { l1: { props: { l2: { type: "boolean"; }; invalid: boolean; }; }; }; }
19-
20-
props: {
21-
@@= skipped -56, +56 lines =@@
3+
@@= skipped -181, +181 lines =@@
224
>1 : T
235

246
0: T & { children: BuildTree<T, N, Prepend<any, I>>[] };

testdata/baselines/reference/submodule/compiler/excessPropertyCheckWithMultipleDiscriminants.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ excessPropertyCheckWithMultipleDiscriminants.ts(30,5): error TS2353: Object lite
22
excessPropertyCheckWithMultipleDiscriminants.ts(41,5): error TS2353: Object literal may only specify known properties, and 'p3' does not exist in type '{ p1: "left"; p2: boolean; }'.
33
excessPropertyCheckWithMultipleDiscriminants.ts(50,5): error TS2353: Object literal may only specify known properties, and 'p4' does not exist in type '{ p1: "left"; p2: true; p3: number; } | { p1: "left"; p2: boolean; }'.
44
excessPropertyCheckWithMultipleDiscriminants.ts(57,5): error TS2353: Object literal may only specify known properties, and 'p3' does not exist in type '{ p1: "right"; p2: false; p4: string; }'.
5-
excessPropertyCheckWithMultipleDiscriminants.ts(83,5): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'Common | Common & A'.
5+
excessPropertyCheckWithMultipleDiscriminants.ts(83,5): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'Common | (Common & A)'.
66
excessPropertyCheckWithMultipleDiscriminants.ts(93,5): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'A | Common'.
77
excessPropertyCheckWithMultipleDiscriminants.ts(131,5): error TS2353: Object literal may only specify known properties, and 'autoIncrement' does not exist in type 'OneToOneAttribute | StringAttribute'.
88
excessPropertyCheckWithMultipleDiscriminants.ts(137,5): error TS2353: Object literal may only specify known properties, and 'autoIncrement' does not exist in type 'StringAttribute'.
@@ -101,7 +101,7 @@ excessPropertyCheckWithMultipleDiscriminants.ts(137,5): error TS2353: Object lit
101101
a: 1,
102102
b: 1 // excess property
103103
~
104-
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'Common | Common & A'.
104+
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'Common | (Common & A)'.
105105
}
106106

107107
type CommonWithDisjointOverlappingOptionals = Common | A | B;

testdata/baselines/reference/submodule/compiler/excessPropertyCheckWithMultipleDiscriminants.errors.txt.diff

Lines changed: 0 additions & 20 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/inDoesNotOperateOnPrimitiveTypes.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function union5<T extends object | string, U extends object | number>(p: T | U)
148148
"key" in p;
149149
>"key" in p : boolean
150150
>"key" : "key"
151-
>p : T & object | U & object
151+
>p : (T & object) | (U & object)
152152
}
153153
}
154154

0 commit comments

Comments
 (0)