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): [consistent-type-assertions] always allow const assertions #1713

Merged
merged 3 commits into from Apr 12, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
Expand Up @@ -8,6 +8,8 @@ Type assertions are also commonly referred as "type casting" in TypeScript (even

In addition to ensuring that type assertions are written in a consistent way, this rule also helps make your codebase more type-safe.

`const` assertions, [introduced in TypeScript 3.4](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions), is always allowed by this rule. Examples of it include `let x = "hello" as const;` and `let x = <const>"hello";`.

## Options

```ts
Expand Down
20 changes: 18 additions & 2 deletions packages/eslint-plugin/src/rules/consistent-type-assertions.ts
Expand Up @@ -75,10 +75,27 @@ export default util.createRule<Options, MessageIds>({
create(context, [options]) {
const sourceCode = context.getSourceCode();

function isConst(node: TSESTree.TypeNode): boolean {
if (node.type !== AST_NODE_TYPES.TSTypeReference) {
return false;
}

return (
node.typeName.type === AST_NODE_TYPES.Identifier &&
node.typeName.name === 'const'
);
}

function reportIncorrectAssertionType(
node: TSESTree.TSTypeAssertion | TSESTree.TSAsExpression,
): void {
// If this node is `as const`, then don't report an error.
if (isConst(node.typeAnnotation)) {
return;
}

const messageId = options.assertionStyle;

context.report({
node,
messageId,
Expand All @@ -97,8 +114,7 @@ export default util.createRule<Options, MessageIds>({
case AST_NODE_TYPES.TSTypeReference:
return (
// Ignore `as const` and `<const>`
(node.typeName.type === AST_NODE_TYPES.Identifier &&
node.typeName.name !== 'const') ||
!isConst(node) ||
// Allow qualified names which have dots between identifiers, `Foo.Bar`
node.typeName.type === AST_NODE_TYPES.TSQualifiedName
);
Expand Down
Expand Up @@ -9,14 +9,12 @@ const ANGLE_BRACKET_TESTS = `
const x = <Foo>new Generic<int>();
const x = <A>b;
const x = <readonly number[]>[1];
const x = <const>[1];
const x = <a | b>('string');
`;
const AS_TESTS = `
const x = new Generic<int>() as Foo;
const x = b as A;
const x = [1] as readonly number[];
const x = [1] as const;
const x = ('string') as a | b;
`;
const OBJECT_LITERAL_AS_CASTS = `
Expand Down Expand Up @@ -98,6 +96,22 @@ ruleTester.run('consistent-type-assertions', rule, {
},
],
}),
{
code: 'const x = <const>[1];',
options: [
{
assertionStyle: 'never',
},
],
},
{
code: 'const x = [1] as const;',
options: [
{
assertionStyle: 'never',
},
],
},
],
invalid: [
...batchedSingleLineTests({
Expand Down