Skip to content

feat(eslint-plugin): [no-unnecessary-type-assertion] add option to ignore string const assertions #10979

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

Merged
merged 5 commits into from
Apr 21, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ type Foo = number;
const foo = (3 + 5) as Foo;
```

```ts
const foo = 'foo' as const;
```

```ts
function foo(x: number): number {
return x!; // unnecessary non-null
Expand Down Expand Up @@ -73,6 +69,18 @@ function foo(x: number | undefined): number {

## Options

### `checkLiteralConstAssertions`

{/* insert option description */}

With `@typescript-eslint/no-unnecessary-type-assertion: ["error", { checkLiteralConstAssertions: true }]`, the following is **incorrect** code:

```ts option='{ "checkLiteralConstAssertions": true }' showPlaygroundButton
const foo = 'foo' as const;
```

See [#8721 False positives for "as const" assertions (issue comment)](https://github.com/typescript-eslint/typescript-eslint/issues/8721#issuecomment-2145291966) for more information on this option.

### `typesToIgnore`

{/* insert option description */}
Expand Down
27 changes: 24 additions & 3 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {

export type Options = [
{
checkLiteralConstAssertions?: boolean;
typesToIgnore?: string[];
},
];
Expand Down Expand Up @@ -48,6 +49,10 @@ export default createRule<Options, MessageIds>({
type: 'object',
additionalProperties: false,
properties: {
checkLiteralConstAssertions: {
type: 'boolean',
description: 'Whether to check literal const assertions.',
},
typesToIgnore: {
type: 'array',
description: 'A list of type names to ignore.',
Expand Down Expand Up @@ -217,6 +222,10 @@ export default createRule<Options, MessageIds>({
return false;
}

function isTypeLiteral(type: ts.Type) {
return type.isLiteral() || tsutils.isBooleanLiteralType(type);
}

return {
'TSAsExpression, TSTypeAssertion'(
node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion,
Expand All @@ -230,12 +239,24 @@ export default createRule<Options, MessageIds>({
}

const castType = services.getTypeAtLocation(node);
const castTypeIsLiteral = isTypeLiteral(castType);
const typeAnnotationIsConstAssertion = isConstAssertion(
node.typeAnnotation,
);

if (
!options.checkLiteralConstAssertions &&
castTypeIsLiteral &&
typeAnnotationIsConstAssertion
) {
return;
}

const uncastType = services.getTypeAtLocation(node.expression);
const typeIsUnchanged = isTypeUnchanged(uncastType, castType);

const wouldSameTypeBeInferred = castType.isLiteral()
const wouldSameTypeBeInferred = castTypeIsLiteral
? isImplicitlyNarrowedLiteralDeclaration(node)
: !isConstAssertion(node.typeAnnotation);
: !typeAnnotationIsConstAssertion;

if (typeIsUnchanged && wouldSameTypeBeInferred) {
context.report({
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

173 changes: 120 additions & 53 deletions packages/eslint-plugin/tests/rules/no-unnecessary-type-assertion.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,25 +429,35 @@ declare function foo<T extends unknown>(bar: T): T;
const baz: unknown = {};
foo(baz!);
`,
],

invalid: [
// https://github.com/typescript-eslint/typescript-eslint/issues/8737
{
code: 'const a = `a` as const;',
errors: [{ line: 1, messageId: 'unnecessaryAssertion' }],
output: 'const a = `a`;',
},
{
code: "const a = 'a' as const;",
errors: [{ line: 1, messageId: 'unnecessaryAssertion' }],
output: "const a = 'a';",
},
{
code: "const a = <const>'a';",
errors: [{ line: 1, messageId: 'unnecessaryAssertion' }],
output: "const a = 'a';",
},
{
code: `
class T {
readonly a = 'a' as const;
}
`,
},
{
code: `
enum T {
Value1,
Value2,
}
declare const a: T.Value1;
const b = a as const;
`,
},
],

invalid: [
{
code: 'const foo = <3>3;',
errors: [{ column: 13, line: 1, messageId: 'unnecessaryAssertion' }],
Expand Down Expand Up @@ -1209,24 +1219,6 @@ var x = 1;
},
{
code: `
class T {
readonly a = 'a' as const;
}
`,
errors: [
{
line: 3,
messageId: 'unnecessaryAssertion',
},
],
output: `
class T {
readonly a = 'a';
}
`,
},
{
code: `
class T {
readonly a = 3 as 3;
}
Expand Down Expand Up @@ -1319,31 +1311,6 @@ enum T {
Value2,
}

declare const a: T.Value1;
const b = a;
`,
},
{
code: `
enum T {
Value1,
Value2,
}

declare const a: T.Value1;
const b = a as const;
`,
errors: [
{
messageId: 'unnecessaryAssertion',
},
],
output: `
enum T {
Value1,
Value2,
}

declare const a: T.Value1;
const b = a;
`,
Expand Down Expand Up @@ -1380,5 +1347,105 @@ const baz: unknown = {};
foo(baz);
`,
},
{
code: 'const a = true as const;',
errors: [{ line: 1, messageId: 'unnecessaryAssertion' }],
options: [{ checkLiteralConstAssertions: true }],
output: 'const a = true;',
},
{
code: 'const a = <const>true;',
errors: [{ line: 1, messageId: 'unnecessaryAssertion' }],
options: [{ checkLiteralConstAssertions: true }],
output: 'const a = true;',
},
{
code: 'const a = 1 as const;',
errors: [{ line: 1, messageId: 'unnecessaryAssertion' }],
options: [{ checkLiteralConstAssertions: true }],
output: 'const a = 1;',
},
{
code: 'const a = <const>1;',
errors: [{ line: 1, messageId: 'unnecessaryAssertion' }],
options: [{ checkLiteralConstAssertions: true }],
output: 'const a = 1;',
},
{
code: 'const a = 1n as const;',
errors: [{ line: 1, messageId: 'unnecessaryAssertion' }],
options: [{ checkLiteralConstAssertions: true }],
output: 'const a = 1n;',
},
{
code: 'const a = <const>1n;',
errors: [{ line: 1, messageId: 'unnecessaryAssertion' }],
options: [{ checkLiteralConstAssertions: true }],
output: 'const a = 1n;',
},
// https://github.com/typescript-eslint/typescript-eslint/issues/8737
{
code: 'const a = `a` as const;',
errors: [{ line: 1, messageId: 'unnecessaryAssertion' }],
options: [{ checkLiteralConstAssertions: true }],
output: 'const a = `a`;',
},
{
code: "const a = 'a' as const;",
errors: [{ line: 1, messageId: 'unnecessaryAssertion' }],
options: [{ checkLiteralConstAssertions: true }],
output: "const a = 'a';",
},
{
code: "const a = <const>'a';",
errors: [{ line: 1, messageId: 'unnecessaryAssertion' }],
options: [{ checkLiteralConstAssertions: true }],
output: "const a = 'a';",
},
{
code: `
class T {
readonly a = 'a' as const;
}
`,
errors: [
{
line: 3,
messageId: 'unnecessaryAssertion',
},
],
options: [{ checkLiteralConstAssertions: true }],
output: `
class T {
readonly a = 'a';
}
`,
},
{
code: `
enum T {
Value1,
Value2,
}

declare const a: T.Value1;
const b = a as const;
`,
errors: [
{
messageId: 'unnecessaryAssertion',
},
],
options: [{ checkLiteralConstAssertions: true }],
output: `
enum T {
Value1,
Value2,
}

declare const a: T.Value1;
const b = a;
`,
},
],
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading