Skip to content
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

fix(eslint-plugin): [no-type-alias] consider type imports as alias types #3433

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/rules/no-type-alias.ts
Expand Up @@ -122,6 +122,7 @@ export default util.createRule<Options, MessageIds>({
];
const aliasTypes = new Set([
AST_NODE_TYPES.TSArrayType,
AST_NODE_TYPES.TSImportType,
AST_NODE_TYPES.TSTypeReference,
AST_NODE_TYPES.TSLiteralType,
AST_NODE_TYPES.TSTypeQuery,
Expand Down
45 changes: 45 additions & 0 deletions packages/eslint-plugin/tests/rules/no-type-alias.test.ts
Expand Up @@ -394,6 +394,10 @@ export type ClassValue =
code: 'type Foo = typeof bar;',
options: [{ allowAliases: 'always' }],
},
{
code: "type Foo = typeof import('foo');",
options: [{ allowAliases: 'always' }],
},
{
code: `
const WithAKey = { AKey: true };
Expand All @@ -405,6 +409,10 @@ type KeyNames = keyof typeof SCALARS;
code: 'type Foo = typeof bar | typeof baz;',
options: [{ allowAliases: 'in-unions' }],
},
{
code: "type Foo = typeof bar | typeof import('foo');",
options: [{ allowAliases: 'in-unions' }],
},
{
code: 'type Foo = keyof [string];',
options: [{ allowTupleTypes: 'always' }],
Expand Down Expand Up @@ -505,6 +513,20 @@ type KeyNames = keyof typeof SCALARS;
},
],
},
{
code: "type Foo = typeof import('foo');",
options: [{ allowAliases: 'never' }],
errors: [
{
messageId: 'noTypeAlias',
data: {
alias: 'aliases',
},
line: 1,
column: 12,
},
],
},
{
code: "type Foo = 'a' | 'b';",
errors: [
Expand All @@ -528,6 +550,29 @@ type KeyNames = keyof typeof SCALARS;
},
],
},
{
code: "type Foo = 'a' | typeof import('foo');",
errors: [
{
messageId: 'noCompositionAlias',
data: {
typeName: 'Aliases',
compositionType: 'union',
},
line: 1,
column: 12,
},
{
messageId: 'noCompositionAlias',
data: {
typeName: 'Aliases',
compositionType: 'union',
},
line: 1,
column: 18,
},
],
},
{
code: "type Foo = 'a' | 'b';",
options: [{ allowLiterals: 'in-unions' }],
Expand Down