Skip to content

Commit

Permalink
feat(eslint-plugin): [no-type-alias] handle conditional types (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
otofu-square authored and bradzacher committed Nov 14, 2019
1 parent 9c8203f commit 259ff20
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
11 changes: 11 additions & 0 deletions packages/eslint-plugin/docs/rules/no-type-alias.md
Expand Up @@ -84,6 +84,7 @@ or more of the following you may pass an object with the options set as follows:

- `allowAliases` set to `"always"` will allow you to do aliasing (Defaults to `"never"`).
- `allowCallbacks` set to `"always"` will allow you to use type aliases with callbacks (Defaults to `"never"`)
- `allowConditionalTypes` set to `"always"` will allow you to use type aliases with conditional types (Defaults to `"never"`)
- `allowConstructors` set to `"always"` will allow you to use type aliases with constructors (Defaults to `"never"`)
- `allowLiterals` set to `"always"` will allow you to use type aliases with literal objects (Defaults to `"never"`)
- `allowMappedTypes` set to `"always"` will allow you to use type aliases as mapping tools (Defaults to `"never"`)
Expand Down Expand Up @@ -249,6 +250,16 @@ type Foo = (name: string, age: number) => string | Person;
type Foo = (name: string, age: number) => string & Person;
```

### allowConditionalTypes

This applies to conditional types.

Examples of **correct** code for the `{ "allowConditionalTypes": "always" }` option:

```ts
type Foo<T> = T extends number ? number : null;
```

### allowConstructors

This applies to constructor types.
Expand Down
16 changes: 16 additions & 0 deletions packages/eslint-plugin/src/rules/no-type-alias.ts
Expand Up @@ -22,6 +22,7 @@ type Options = [
{
allowAliases?: Values;
allowCallbacks?: 'always' | 'never';
allowConditionalTypes?: 'always' | 'never';
allowConstructors?: 'always' | 'never';
allowLiterals?: Values;
allowMappedTypes?: Values;
Expand Down Expand Up @@ -63,6 +64,9 @@ export default util.createRule<Options, MessageIds>({
allowCallbacks: {
enum: ['always', 'never'],
},
allowConditionalTypes: {
enum: ['always', 'never'],
},
allowConstructors: {
enum: ['always', 'never'],
},
Expand All @@ -84,6 +88,7 @@ export default util.createRule<Options, MessageIds>({
{
allowAliases: 'never',
allowCallbacks: 'never',
allowConditionalTypes: 'never',
allowConstructors: 'never',
allowLiterals: 'never',
allowMappedTypes: 'never',
Expand All @@ -96,6 +101,7 @@ export default util.createRule<Options, MessageIds>({
{
allowAliases,
allowCallbacks,
allowConditionalTypes,
allowConstructors,
allowLiterals,
allowMappedTypes,
Expand Down Expand Up @@ -226,6 +232,16 @@ export default util.createRule<Options, MessageIds>({
if (allowCallbacks === 'never') {
reportError(type.node, type.compositionType, isTopLevel, 'Callbacks');
}
} else if (type.node.type === AST_NODE_TYPES.TSConditionalType) {
// conditional type
if (allowConditionalTypes === 'never') {
reportError(
type.node,
type.compositionType,
isTopLevel,
'Conditional types',
);
}
} else if (type.node.type === AST_NODE_TYPES.TSConstructorType) {
if (allowConstructors === 'never') {
reportError(
Expand Down
35 changes: 33 additions & 2 deletions packages/eslint-plugin/tests/rules/no-type-alias.test.ts
Expand Up @@ -439,6 +439,10 @@ type Foo<T> = {
'type Foo = [string] & [number, number] | keyof [number, number, number];',
options: [{ allowTupleTypes: 'in-unions-and-intersections' }],
},
{
code: 'type MyType<T> = T extends number ? number : null;',
options: [{ allowConditionalTypes: 'always' }],
},
{
code: 'type Foo = new (bar: number) => string | null;',
options: [{ allowConstructors: 'always' }],
Expand Down Expand Up @@ -3188,9 +3192,36 @@ type Foo<T> = {
messageId: 'noTypeAlias',
data: {
alias: 'constructors',
line: 1,
column: 12,
},
line: 1,
column: 12,
},
],
},
{
code: 'type MyType<T> = T extends number ? number : null;',
errors: [
{
messageId: 'noTypeAlias',
data: {
alias: 'conditional types',
},
line: 1,
column: 18,
},
],
},
{
code: 'type MyType<T> = T extends number ? number : null;',
options: [{ allowConditionalTypes: 'never' }],
errors: [
{
messageId: 'noTypeAlias',
data: {
alias: 'conditional types',
},
line: 1,
column: 18,
},
],
},
Expand Down

0 comments on commit 259ff20

Please sign in to comment.