diff --git a/packages/eslint-plugin/src/rules/no-type-alias.ts b/packages/eslint-plugin/src/rules/no-type-alias.ts index 368528d9798..fa4f6314c13 100644 --- a/packages/eslint-plugin/src/rules/no-type-alias.ts +++ b/packages/eslint-plugin/src/rules/no-type-alias.ts @@ -10,6 +10,13 @@ type Values = | 'in-unions' | 'in-intersections' | 'in-unions-and-intersections'; +const enumValues: Values[] = [ + 'always', + 'never', + 'in-unions', + 'in-intersections', + 'in-unions-and-intersections', +]; type Options = [ { @@ -49,61 +56,19 @@ export default util.createRule({ type: 'object', properties: { allowAliases: { - enum: [ - 'always', - 'never', - 'in-unions', - 'in-intersections', - 'in-unions-and-intersections', - ], + enum: enumValues, }, allowCallbacks: { enum: ['always', 'never'], }, allowLiterals: { - enum: [ - 'always', - 'never', - 'in-unions', - 'in-intersections', - 'in-unions-and-intersections', - ], + enum: enumValues, }, allowMappedTypes: { - enum: [ - 'always', - 'never', - 'in-unions', - 'in-intersections', - 'in-unions-and-intersections', - ], + enum: enumValues, }, allowTupleTypes: { - enum: [ - 'always', - 'never', - 'in-unions', - 'in-intersections', - 'in-unions-and-intersections', - ], - }, - allowKeyOf: { - enum: [ - 'always', - 'never', - 'in-unions', - 'in-intersections', - 'in-unions-and-intersections', - ], - }, - allowReadOnly: { - enum: [ - 'always', - 'never', - 'in-unions', - 'in-intersections', - 'in-unions-and-intersections', - ], + enum: enumValues, }, }, additionalProperties: false, 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 3dfa4c565ec..d17348af746 100644 --- a/packages/eslint-plugin/tests/rules/no-type-alias.test.ts +++ b/packages/eslint-plugin/tests/rules/no-type-alias.test.ts @@ -7,10 +7,6 @@ const ruleTester = new RuleTester({ ruleTester.run('no-type-alias', rule, { valid: [ - { - code: 'type Foo = keyof [string]', - options: [{ allowTupleTypes: 'always' }], - }, { code: "type A = 'a' & ('b' | 'c');", options: [{ allowAliases: 'always' }], @@ -380,6 +376,10 @@ type Foo = { code: 'type Foo = typeof bar | typeof baz;', options: [{ allowAliases: 'in-unions' }], }, + { + code: 'type Foo = keyof [string]', + options: [{ allowTupleTypes: 'always' }], + }, { code: 'type Foo = [string] | [number, number];', options: [{ allowTupleTypes: 'always' }], @@ -397,48 +397,48 @@ type Foo = { 'type Foo = [string] & [number, number] | [number, number, number];', options: [{ allowTupleTypes: 'in-unions-and-intersections' }], }, - // { - // code: 'type Foo = readonly [string] | [number, number];', - // options: [{ allowReadOnly: 'always' }], - // }, - // { - // code: 'type Foo = readonly [string] | readonly [number, number];', - // options: [{ allowReadOnly: 'always' }], - // }, - // { - // code: 'type Foo = readonly [string] | [number, number];', - // options: [{ allowReadOnly: 'in-unions' }], - // }, - // { - // code: 'type Foo = [string] & readonly [number, number];', - // options: [{ allowReadOnly: 'in-intersections' }], - // }, - // { - // code: - // 'type Foo = [string] & [number, number] | readonly [number, number, number];', - // options: [{ allowReadOnly: 'in-unions-and-intersections' }], - // }, - // { - // code: 'type Foo = keyof [string] | [number, number];', - // options: [{ allowKeyOf: 'always' }], - // }, - // { - // code: 'type Foo = keyof [string] | keyof [number, number];', - // options: [{ allowKeyOf: 'always' }], - // }, - // { - // code: 'type Foo = keyof [string] | [number, number];', - // options: [{ allowKeyOf: 'in-unions' }], - // }, - // { - // code: 'type Foo = [string] & keyof [number, number];', - // options: [{ allowKeyOf: 'in-intersections' }], - // }, - // { - // code: - // 'type Foo = [string] & [number, number] | keyof [number, number, number];', - // options: [{ allowKeyOf: 'in-unions-and-intersections' }], - // }, + { + code: 'type Foo = readonly [string] | [number, number];', + options: [{ allowTupleTypes: 'always' }], + }, + { + code: 'type Foo = readonly [string] | readonly [number, number];', + options: [{ allowTupleTypes: 'always' }], + }, + { + code: 'type Foo = readonly [string] | [number, number];', + options: [{ allowTupleTypes: 'in-unions' }], + }, + { + code: 'type Foo = [string] & readonly [number, number];', + options: [{ allowTupleTypes: 'in-intersections' }], + }, + { + code: + 'type Foo = [string] & [number, number] | readonly [number, number, number];', + options: [{ allowTupleTypes: 'in-unions-and-intersections' }], + }, + { + code: 'type Foo = keyof [string] | [number, number];', + options: [{ allowTupleTypes: 'always' }], + }, + { + code: 'type Foo = keyof [string] | keyof [number, number];', + options: [{ allowTupleTypes: 'always' }], + }, + { + code: 'type Foo = keyof [string] | [number, number];', + options: [{ allowTupleTypes: 'in-unions' }], + }, + { + code: 'type Foo = [string] & keyof [number, number];', + options: [{ allowTupleTypes: 'in-intersections' }], + }, + { + code: + 'type Foo = [string] & [number, number] | keyof [number, number, number];', + options: [{ allowTupleTypes: 'in-unions-and-intersections' }], + }, ], invalid: [ { @@ -3077,5 +3077,104 @@ type Foo = { }, ], }, + { + code: 'type Foo = readonly [number] | keyof [number, number]', + options: [{ allowTupleTypes: 'never' }], + errors: [ + { + messageId: 'noCompositionAlias', + data: { + compositionType: 'union', + typeName: 'Tuple Types', + }, + line: 1, + column: 12, + }, + { + messageId: 'noCompositionAlias', + data: { + compositionType: 'union', + typeName: 'Tuple Types', + }, + line: 1, + column: 32, + }, + ], + }, + { + code: 'type Foo = keyof [number] & [number, number]', + options: [{ allowTupleTypes: 'in-unions' }], + errors: [ + { + messageId: 'noCompositionAlias', + data: { + compositionType: 'intersection', + typeName: 'Tuple Types', + }, + line: 1, + column: 12, + }, + { + messageId: 'noCompositionAlias', + data: { + compositionType: 'intersection', + typeName: 'Tuple Types', + }, + line: 1, + column: 29, + }, + ], + }, + { + code: 'type Foo = [number] | readonly [number, number]', + options: [{ allowTupleTypes: 'in-intersections' }], + errors: [ + { + messageId: 'noCompositionAlias', + data: { + compositionType: 'union', + typeName: 'Tuple Types', + }, + line: 1, + column: 12, + }, + { + messageId: 'noCompositionAlias', + data: { + compositionType: 'union', + typeName: 'Tuple Types', + }, + line: 1, + column: 23, + }, + ], + }, + { + code: 'type Foo = readonly [number];', + options: [{ allowTupleTypes: 'in-intersections' }], + errors: [ + { + messageId: 'noTypeAlias', + }, + ], + }, + { + code: 'type Foo = keyof [number];', + options: [{ allowTupleTypes: 'in-unions' }], + errors: [ + { + messageId: 'noTypeAlias', + }, + ], + }, + { + code: 'type Foo = readonly [number];', + options: [{ allowTupleTypes: 'in-unions-and-intersections' }], + errors: [ + { + messageId: 'noTypeAlias', + }, + ], + }, ], });