diff --git a/packages/eslint-plugin/docs/rules/no-type-alias.md b/packages/eslint-plugin/docs/rules/no-type-alias.md index 734c6de8d3f..f9b4f81b837 100644 --- a/packages/eslint-plugin/docs/rules/no-type-alias.md +++ b/packages/eslint-plugin/docs/rules/no-type-alias.md @@ -118,6 +118,8 @@ type Foo = string | string[]; type Foo = string & string[]; +type Foo = `foo-${number}`; + // reference types interface Bar {} class Baz implements Bar {} @@ -139,6 +141,8 @@ type Foo = string; type Foo = string & string[]; +type Foo = `foo-${number}`; + // reference types interface Bar {} class Baz implements Bar {} @@ -156,6 +160,8 @@ type Foo = 'a' | 'b'; type Foo = string | string[]; +type Foo = `a-${number}` | `b-${number}`; + // reference types interface Bar {} class Baz implements Bar {} @@ -175,6 +181,8 @@ type Foo = string; type Foo = string | string[]; +type Foo = `a-${number}` | `b-${number}`; + // reference types interface Bar {} class Baz implements Bar {} @@ -190,6 +198,8 @@ Examples of **correct** code for the `{ "allowAliases": "in-intersections" }` op // primitives type Foo = string & string[]; +type Foo = `a-${number}` & `b-${number}`; + // reference types interface Bar {} class Baz implements Bar {} @@ -205,6 +215,8 @@ type Foo = 'a'; type Foo = string; +type Foo = `foo-${number}`; + // reference types interface Bar {} class Baz implements Bar {} @@ -222,6 +234,10 @@ type Foo = string | string[]; type Foo = string & string[]; +type Foo = `a-${number}` & `b-${number}`; + +type Foo = `a-${number}` | `b-${number}`; + // reference types interface Bar {} class Baz implements Bar {} diff --git a/packages/eslint-plugin/src/rules/no-type-alias.ts b/packages/eslint-plugin/src/rules/no-type-alias.ts index 2d9847eb309..fdd226ca41c 100644 --- a/packages/eslint-plugin/src/rules/no-type-alias.ts +++ b/packages/eslint-plugin/src/rules/no-type-alias.ts @@ -133,6 +133,7 @@ export default util.createRule({ AST_NODE_TYPES.TSLiteralType, AST_NODE_TYPES.TSTypeQuery, AST_NODE_TYPES.TSIndexedAccessType, + AST_NODE_TYPES.TSTemplateLiteralType, ]); /** diff --git a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts index f0da2160b63..6a307454f51 100644 --- a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts +++ b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts @@ -131,6 +131,66 @@ ruleTester.run('no-type-alias', rule, { code: 'type Foo = 1 | (2 & 3);', options: [{ allowAliases: 'in-unions-and-intersections' }], }, + { + code: 'type Foo = `a-${number}`;', + options: [{ allowAliases: 'always' }], + }, + { + code: 'type Foo = `a-${number}` | `b-${number}`;', + options: [{ allowAliases: 'always' }], + }, + { + code: 'type Foo = `a-${number}` | `b-${number}`;', + options: [{ allowAliases: 'in-unions-and-intersections' }], + }, + { + code: 'type Foo = `a-${number}` | `b-${number}`;', + options: [{ allowAliases: 'in-unions' }], + }, + { + code: 'type Foo = `a-${number}` | `b-${number}` | `c-${number}`;', + options: [{ allowAliases: 'always' }], + }, + { + code: 'type Foo = `a-${number}` | `b-${number}` | `c-${number}`;', + options: [{ allowAliases: 'in-unions-and-intersections' }], + }, + { + code: 'type Foo = `a-${number}` | `b-${number}` | `c-${number}`;', + options: [{ allowAliases: 'in-unions' }], + }, + { + code: 'type Foo = `a-${number}` & `b-${number}`;', + options: [{ allowAliases: 'always' }], + }, + { + code: 'type Foo = `a-${number}` & `b-${number}`;', + options: [{ allowAliases: 'in-unions-and-intersections' }], + }, + { + code: 'type Foo = `a-${number}` & `b-${number}`;', + options: [{ allowAliases: 'in-intersections' }], + }, + { + code: 'type Foo = `a-${number}` & `b-${number}` & `c-${number}`;', + options: [{ allowAliases: 'always' }], + }, + { + code: 'type Foo = `a-${number}` & `b-${number}` & `c-${number}`;', + options: [{ allowAliases: 'in-unions-and-intersections' }], + }, + { + code: 'type Foo = `a-${number}` & `b-${number}` & `c-${number}`;', + options: [{ allowAliases: 'in-intersections' }], + }, + { + code: 'type Foo = `a-${number}` | (`b-${number}` & `c-${number}`);', + options: [{ allowAliases: 'always' }], + }, + { + code: 'type Foo = `a-${number}` | (`b-${number}` & `c-${number}`);', + options: [{ allowAliases: 'in-unions-and-intersections' }], + }, { code: 'type Foo = true;', options: [{ allowAliases: 'always' }], @@ -3340,5 +3400,66 @@ type Foo = { }, ], }, + { + code: 'type Foo = `foo-${number}`;', + errors: [ + { + messageId: 'noTypeAlias', + data: { + alias: 'aliases', + }, + line: 1, + column: 12, + }, + ], + }, + { + code: 'type Foo = `a-${number}` | `b-${number}`;', + options: [{ allowAliases: 'never' }], + errors: [ + { + messageId: 'noCompositionAlias', + data: { + typeName: 'Aliases', + compositionType: 'union', + }, + line: 1, + column: 12, + }, + { + messageId: 'noCompositionAlias', + data: { + typeName: 'Aliases', + compositionType: 'union', + }, + line: 1, + column: 28, + }, + ], + }, + { + code: 'type Foo = `a-${number}` & `b-${number}`;', + options: [{ allowAliases: 'never' }], + errors: [ + { + messageId: 'noCompositionAlias', + data: { + typeName: 'Aliases', + compositionType: 'intersection', + }, + line: 1, + column: 12, + }, + { + messageId: 'noCompositionAlias', + data: { + typeName: 'Aliases', + compositionType: 'intersection', + }, + line: 1, + column: 28, + }, + ], + }, ], });