diff --git a/packages/eslint-plugin/src/util/misc.ts b/packages/eslint-plugin/src/util/misc.ts index fa9c5ccf528..8362736bd62 100644 --- a/packages/eslint-plugin/src/util/misc.ts +++ b/packages/eslint-plugin/src/util/misc.ts @@ -210,6 +210,7 @@ function typeNodeRequiresParentheses( return ( node.type === AST_NODE_TYPES.TSFunctionType || node.type === AST_NODE_TYPES.TSConstructorType || + node.type === AST_NODE_TYPES.TSConditionalType || (node.type === AST_NODE_TYPES.TSUnionType && text.startsWith('|')) || (node.type === AST_NODE_TYPES.TSIntersectionType && text.startsWith('&')) ); diff --git a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts index 1aa6f8a6c9a..42f9ab8153a 100644 --- a/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts +++ b/packages/eslint-plugin/tests/rules/sort-type-constituents.test.ts @@ -359,6 +359,7 @@ type T = 1 | string | {} | A; }, ], }, + "type A = string | (T extends number ? 'hi' : 'there');", ], invalid: [ ...invalid('|'), @@ -376,5 +377,18 @@ type T = 1 | string | {} | A; }, ], }, + { + output: "type A = string | (T extends number ? 'hi' : 'there');", + code: "type A = (T extends number ? 'hi' : 'there') | string;", + errors: [ + { + messageId: 'notSortedNamed', + data: { + type: 'Union', + name: 'A', + }, + }, + ], + }, ], });