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

feat(eslint-plugin): [no-type-alias] allowTemplateLiterals #5097

Closed
Closed
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
56 changes: 56 additions & 0 deletions packages/eslint-plugin/docs/rules/no-type-alias.md
Expand Up @@ -92,6 +92,7 @@ or more of the following you may pass an object with the options set as follows:
- `allowMappedTypes` set to `"always"` will allow you to use type aliases as mapping tools (Defaults to `"never"`)
- `allowTupleTypes` set to `"always"` will allow you to use type aliases with tuples (Defaults to `"never"`)
- `allowGenerics` set to `"always"` will allow you to use type aliases with generics (Defaults to `"never"`)
- `allowTemplateLiterals` set to `"always"` will allow you to use type aliases with template literals (Defaults to `"never"`)

### `allowAliases`

Expand Down Expand Up @@ -600,6 +601,61 @@ type Foo = Partial<Bar>;
type Foo = Omit<Bar, 'a' | 'b'>;
```

### `allowTemplateLiterals`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should mention that the option falls back to the value for allowAliases. That's what I would expect.


This applies to template literals.

The setting accepts the following values:

- `"always"` or `"never"` to active or deactivate the feature.
- `"in-unions"`
- `"in-intersections"`
- `"in-unions-and-intersections"`

Examples of **correct** code for the `{ "allowTemplateLiterals": "always" }` options:

```ts
type Foo = `foo-${number}`;
```

Examples of **incorrect** code for the `{ "allowTemplateLiterals": "in-unions" }` option:

```ts
type Foo = `foo-${number}`;
```

Examples of **correct** code for the `{ "allowTemplateLiterals": "in-unions" }` option:

```ts
type Foo = `a-${number}` | `b-${number}`;
```

Examples of **incorrect** code for the `{ "allowTemplateLiterals": "in-intersections" }` option:

```ts
type Foo = `a-${number}` | `b-${number}`;
```

Examples of **correct** code for the `{ "allowTemplateLiterals": "in-intersections" }` option:

```ts
type Foo = `a-${number}` & `b-${number}`;
```

Examples of **incorrect** code for the `{ "allowTemplateLiterals": "in-unions-and-intersections" }` option:

```ts
type Foo = `foo-${number}`;
```

Examples of **correct** code for the `{ "allowTemplateLiterals": "in-unions-and-intersections" }` option:

```ts
type Foo = `a-${number}` & `b-${number}`;

type Foo = `a-${number}` | `b-${number}`;
```

## When Not To Use It

When you can't express some shape with an interface or you need to use a union, tuple type,
Expand Down
13 changes: 13 additions & 0 deletions packages/eslint-plugin/src/rules/no-type-alias.ts
Expand Up @@ -29,6 +29,7 @@ type Options = [
allowMappedTypes?: Values;
allowTupleTypes?: Values;
allowGenerics?: 'always' | 'never';
allowTemplateLiterals?: Values;
},
];
type MessageIds = 'noTypeAlias' | 'noCompositionAlias';
Expand Down Expand Up @@ -83,6 +84,9 @@ export default util.createRule<Options, MessageIds>({
allowGenerics: {
enum: ['always', 'never'],
},
allowTemplateLiterals: {
enum: enumValues,
},
},
additionalProperties: false,
},
Expand All @@ -98,6 +102,7 @@ export default util.createRule<Options, MessageIds>({
allowMappedTypes: 'never',
allowTupleTypes: 'never',
allowGenerics: 'never',
allowTemplateLiterals: 'never',
},
],
create(
Expand All @@ -112,6 +117,7 @@ export default util.createRule<Options, MessageIds>({
allowMappedTypes,
allowTupleTypes,
allowGenerics,
allowTemplateLiterals,
},
],
) {
Expand Down Expand Up @@ -271,6 +277,13 @@ export default util.createRule<Options, MessageIds>({
} else if (type.node.type === AST_NODE_TYPES.TSMappedType) {
// mapped type
checkAndReport(allowMappedTypes!, isTopLevel, type, 'Mapped types');
} else if (type.node.type === AST_NODE_TYPES.TSTemplateLiteralType) {
checkAndReport(
allowTemplateLiterals!,
isTopLevel,
type,
'Template literals',
);
} else if (isValidTupleType(type)) {
// tuple types
checkAndReport(allowTupleTypes!, isTopLevel, type, 'Tuple Types');
Expand Down