Skip to content

fix(eslint-plugin): [no-unnecessary-type-assertion] combine template literal check with const variable check #8820

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
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
30 changes: 15 additions & 15 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,23 @@ export default createRule<Options, MessageIds>({
);
}

function isLiteralVariableDeclarationChangingTypeWithConst(
node: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion,
): boolean {
/**
* If the type assertion is on a template literal WITH expressions we
* should keep the `const` casting
* @see https://github.com/typescript-eslint/typescript-eslint/issues/8737
*/
if (node.expression.type === AST_NODE_TYPES.TemplateLiteral) {
return node.expression.expressions.length === 0;
}

function isImplicitlyNarrowedConstDeclaration({
expression,
parent,
}: TSESTree.TSAsExpression | TSESTree.TSTypeAssertion): boolean {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const maybeDeclarationNode = node.parent.parent!;
const maybeDeclarationNode = parent.parent!;
const isTemplateLiteralWithExpressions =
expression.type === AST_NODE_TYPES.TemplateLiteral &&
expression.expressions.length !== 0;
return (
maybeDeclarationNode.type === AST_NODE_TYPES.VariableDeclaration &&
maybeDeclarationNode.kind === 'const'
maybeDeclarationNode.kind === 'const' &&
/**
* Even on `const` variable declarations, template literals with expressions can sometimes be widened without a type assertion.
* @see https://github.com/typescript-eslint/typescript-eslint/issues/8737
*/
!isTemplateLiteralWithExpressions
);
}

Expand Down Expand Up @@ -267,7 +267,7 @@ export default createRule<Options, MessageIds>({
const typeIsUnchanged = isTypeUnchanged(uncastType, castType);

const wouldSameTypeBeInferred = castType.isLiteral()
? isLiteralVariableDeclarationChangingTypeWithConst(node)
? isImplicitlyNarrowedConstDeclaration(node)
: !isConstAssertion(node.typeAnnotation);

if (typeIsUnchanged && wouldSameTypeBeInferred) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -284,14 +284,15 @@ function bar(items: string[]) {
},
// https://github.com/typescript-eslint/typescript-eslint/issues/8737
`
const myString = 'foo';
let myString = 'foo';
const templateLiteral = \`\${myString}-somethingElse\` as const;
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/8737
`
const myString = 'foo';
let myString = 'foo';
const templateLiteral = <const>\`\${myString}-somethingElse\`;
`,
'let a = `a` as const;',
{
code: `
declare const foo: {
Expand Down
Loading