Skip to content

Print parenthesis around intersections in unions #1240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,7 @@ func (p *Printer) emitNamedTupleMember(node *ast.NamedTupleMember) {
}

func (p *Printer) emitUnionTypeConstituent(node *ast.TypeNode) {
p.emitTypeNode(node, ast.TypePrecedenceIntersection)
p.emitTypeNode(node, ast.TypePrecedenceTypeOperator)
}

func (p *Printer) emitUnionType(node *ast.UnionTypeNode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ interface MachineConfig<TEvent extends { type: string }> {

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

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

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

[K in TEvent["type"] as K extends Uppercase<string> ? K : never]?: Action<TEvent extends { type: K } ? TEvent : never>;
>type : K
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
};
on?: {
->on : ({ [K in TEvent["type"]]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent>; }) | undefined
+>on : { [K in TEvent["type"]]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; } | undefined
+>on : ({ [K in TEvent["type"]]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; }) | undefined

[K in TEvent["type"]]?: Action<TEvent extends { type: K } ? TEvent : never>;
>type : K
Expand All @@ -14,7 +14,7 @@
};
on?: {
->on : ({ [K in TEvent["type"] as K extends Uppercase<string> ? K : never]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent>; }) | undefined
+>on : { [K in TEvent["type"] as K extends Uppercase<string> ? K : never]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; } | undefined
+>on : ({ [K in TEvent["type"] as K extends Uppercase<string> ? K : never]?: Action<TEvent extends { type: K; } ? TEvent : never> | undefined; } & { "*"?: Action<TEvent> | undefined; }) | undefined

[K in TEvent["type"] as K extends Uppercase<string> ? K : never]?: Action<TEvent extends { type: K } ? TEvent : never>;
>type : K
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
excessPropertyCheckIntersectionWithRecursiveType.ts(13,9): error TS2353: Object literal may only specify known properties, and 'invalid' does not exist in type '{ l2: Schema1<boolean>; }'.
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>; }'.
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>); }'.
excessPropertyCheckIntersectionWithRecursiveType.ts(39,9): error TS2353: Object literal may only specify known properties, and 'invalid' does not exist in type '{ l2: Schema3<boolean>; }'.
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>; }'.
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>); }'.
excessPropertyCheckIntersectionWithRecursiveType.ts(86,11): error TS2353: Object literal may only specify known properties, and 'children' does not exist in type 'User'.
excessPropertyCheckIntersectionWithRecursiveType.ts(102,35): error TS2339: Property 'children' does not exist on type 'User'.

Expand Down Expand Up @@ -37,8 +37,8 @@ excessPropertyCheckIntersectionWithRecursiveType.ts(102,35): error TS2339: Prope
l2: { type: 'boolean' },
invalid: false,
~~~~~~~
!!! 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>; }'.
!!! 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; }>'
!!! 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>); }'.
!!! 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; }>'
},
},
},
Expand Down Expand Up @@ -69,8 +69,8 @@ excessPropertyCheckIntersectionWithRecursiveType.ts(102,35): error TS2339: Prope
l2: { type: 'boolean' },
invalid: false,
~~~~~~~
!!! 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>; }'.
!!! 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>; }; }'
!!! 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>); }'.
!!! 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>); }; }'
},
},
},
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type Schema2<T> = (T extends boolean ? { type: 'boolean'; } & Example<T> : { pro
>props : { [P in keyof T]: Schema2<T[P]>; }

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

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

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

props: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
--- old.excessPropertyCheckIntersectionWithRecursiveType.types
+++ new.excessPropertyCheckIntersectionWithRecursiveType.types
@@= skipped -53, +53 lines =@@
>props : { [P in keyof T]: Schema2<T[P]>; }

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

props: {
@@= skipped -72, +72 lines =@@
>props : Example<T> & { [P in keyof T]: Schema4<T[P]>; }

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

props: {
@@= skipped -56, +56 lines =@@
@@= skipped -181, +181 lines =@@
>1 : T

0: T & { children: BuildTree<T, N, Prepend<any, I>>[] };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ excessPropertyCheckWithMultipleDiscriminants.ts(30,5): error TS2353: Object lite
excessPropertyCheckWithMultipleDiscriminants.ts(41,5): error TS2353: Object literal may only specify known properties, and 'p3' does not exist in type '{ p1: "left"; p2: boolean; }'.
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; }'.
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; }'.
excessPropertyCheckWithMultipleDiscriminants.ts(83,5): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'Common | Common & A'.
excessPropertyCheckWithMultipleDiscriminants.ts(83,5): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'Common | (Common & A)'.
excessPropertyCheckWithMultipleDiscriminants.ts(93,5): error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'A | Common'.
excessPropertyCheckWithMultipleDiscriminants.ts(131,5): error TS2353: Object literal may only specify known properties, and 'autoIncrement' does not exist in type 'OneToOneAttribute | StringAttribute'.
excessPropertyCheckWithMultipleDiscriminants.ts(137,5): error TS2353: Object literal may only specify known properties, and 'autoIncrement' does not exist in type 'StringAttribute'.
Expand Down Expand Up @@ -101,7 +101,7 @@ excessPropertyCheckWithMultipleDiscriminants.ts(137,5): error TS2353: Object lit
a: 1,
b: 1 // excess property
~
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'Common | Common & A'.
!!! error TS2353: Object literal may only specify known properties, and 'b' does not exist in type 'Common | (Common & A)'.
}

type CommonWithDisjointOverlappingOptionals = Common | A | B;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function union5<T extends object | string, U extends object | number>(p: T | U)
"key" in p;
>"key" in p : boolean
>"key" : "key"
>p : T & object | U & object
>p : (T & object) | (U & object)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,4 @@
+>union5 : <T extends string | object, U extends number | object>(p: T | U) => void
>p : T | U

// For consistency, this should probably not be an error, because useful
@@= skipped -25, +25 lines =@@
"key" in p;
>"key" in p : boolean
>"key" : "key"
->p : (T & object) | (U & object)
+>p : T & object | U & object
}
}
// For consistency, this should probably not be an error, because useful
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ class B { b = 0 }
>0 : 0

function f10(obj: A & { x: string } | B) {
>f10 : (obj: B | A & { x: string; }) => void
>obj : B | A & { x: string; }
>f10 : (obj: B | (A & { x: string; })) => void
>obj : B | (A & { x: string; })
>x : string

if (obj instanceof Object) {
>obj instanceof Object : boolean
>obj : B | A & { x: string; }
>obj : B | (A & { x: string; })
>Object : ObjectConstructor

obj; // A & { x: string } | B
>obj : B | A & { x: string; }
>obj : B | (A & { x: string; })
}
else {
obj; // Error
Expand Down
Loading