Skip to content

Commit 1bb4d63

Browse files
a-tarasyukbradzacher
authored andcommitted
fix(eslint-plugin): [no-type-alias] handle constructor aliases (typescript-eslint#1198)
1 parent c5835f3 commit 1bb4d63

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

packages/eslint-plugin/docs/rules/no-type-alias.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ or more of the following you may pass an object with the options set as follows:
8484

8585
- `allowAliases` set to `"always"` will allow you to do aliasing (Defaults to `"never"`).
8686
- `allowCallbacks` set to `"always"` will allow you to use type aliases with callbacks (Defaults to `"never"`)
87+
- `allowConstructors` set to `"always"` will allow you to use type aliases with constructors (Defaults to `"never"`)
8788
- `allowLiterals` set to `"always"` will allow you to use type aliases with literal objects (Defaults to `"never"`)
8889
- `allowMappedTypes` set to `"always"` will allow you to use type aliases as mapping tools (Defaults to `"never"`)
8990
- `allowTupleTypes` set to `"always"` will allow you to use type aliases with tuples (Defaults to `"never"`)
@@ -248,6 +249,20 @@ type Foo = (name: string, age: number) => string | Person;
248249
type Foo = (name: string, age: number) => string & Person;
249250
```
250251

252+
### allowConstructors
253+
254+
This applies to constructor types.
255+
256+
The setting accepts the following values:
257+
258+
- `"always"` or `"never"` to active or deactivate the feature.
259+
260+
Examples of **correct** code for the `{ "allowConstructors": "always" }` option:
261+
262+
```ts
263+
type Foo = new () => void;
264+
```
265+
251266
### allowLiterals
252267

253268
This applies to literal types (`type Foo = { ... }`).

packages/eslint-plugin/src/rules/no-type-alias.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ type Options = [
2222
{
2323
allowAliases?: Values;
2424
allowCallbacks?: 'always' | 'never';
25+
allowConstructors?: 'always' | 'never';
2526
allowLiterals?: Values;
2627
allowMappedTypes?: Values;
2728
allowTupleTypes?: Values;
@@ -62,6 +63,9 @@ export default util.createRule<Options, MessageIds>({
6263
allowCallbacks: {
6364
enum: ['always', 'never'],
6465
},
66+
allowConstructors: {
67+
enum: ['always', 'never'],
68+
},
6569
allowLiterals: {
6670
enum: enumValues,
6771
},
@@ -80,6 +84,7 @@ export default util.createRule<Options, MessageIds>({
8084
{
8185
allowAliases: 'never',
8286
allowCallbacks: 'never',
87+
allowConstructors: 'never',
8388
allowLiterals: 'never',
8489
allowMappedTypes: 'never',
8590
allowTupleTypes: 'never',
@@ -91,6 +96,7 @@ export default util.createRule<Options, MessageIds>({
9196
{
9297
allowAliases,
9398
allowCallbacks,
99+
allowConstructors,
94100
allowLiterals,
95101
allowMappedTypes,
96102
allowTupleTypes,
@@ -220,6 +226,15 @@ export default util.createRule<Options, MessageIds>({
220226
if (allowCallbacks === 'never') {
221227
reportError(type.node, type.compositionType, isTopLevel, 'Callbacks');
222228
}
229+
} else if (type.node.type === AST_NODE_TYPES.TSConstructorType) {
230+
if (allowConstructors === 'never') {
231+
reportError(
232+
type.node,
233+
type.compositionType,
234+
isTopLevel,
235+
'Constructors',
236+
);
237+
}
223238
} else if (type.node.type === AST_NODE_TYPES.TSTypeLiteral) {
224239
// literal object type
225240
checkAndReport(allowLiterals!, isTopLevel, type, 'Literals');

packages/eslint-plugin/tests/rules/no-type-alias.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,10 @@ type Foo<T> = {
439439
'type Foo = [string] & [number, number] | keyof [number, number, number];',
440440
options: [{ allowTupleTypes: 'in-unions-and-intersections' }],
441441
},
442+
{
443+
code: 'type Foo = new (bar: number) => string | null;',
444+
options: [{ allowConstructors: 'always' }],
445+
},
442446
],
443447
invalid: [
444448
{
@@ -3176,5 +3180,19 @@ type Foo<T> = {
31763180
},
31773181
],
31783182
},
3183+
{
3184+
code: 'type Foo = new (bar: number) => string | null;',
3185+
options: [{ allowConstructors: 'never' }],
3186+
errors: [
3187+
{
3188+
messageId: 'noTypeAlias',
3189+
data: {
3190+
alias: 'constructors',
3191+
line: 1,
3192+
column: 12,
3193+
},
3194+
},
3195+
],
3196+
},
31793197
],
31803198
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy