Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(eslint-plugin): [no-unused-vars] no is assigned a value but only used as a type error when it has a same name #11322

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
42 changes: 38 additions & 4 deletions packages/eslint-plugin/src/util/collectUnusedVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import type { TSESTree } from '@typescript-eslint/utils';

import {
DefinitionType,
ESLintScopeVariable,
ImplicitLibVariable,
ScopeType,
Visitor,
Expand Down Expand Up @@ -188,7 +190,7 @@
// basic exported variables
isExported(variable) ||
// variables implicitly exported via a merged declaration
isMergableExported(variable) ||
isMergeableExported(variable) ||
// used variables
isUsedVariable(variable)
) {
Expand Down Expand Up @@ -415,6 +417,29 @@
return false;
}

const exportExceptDefTypes: DefinitionType[] = [
DefinitionType.Variable,
DefinitionType.Type,
];
/**
* In edge cases, the existing used logic does not work.
* When the type and variable name of the variable are the same
* @ref https://github.com/typescript-eslint/typescript-eslint/issues/10658
* @param variable the variable to check
* @returns true if it is an edge case
*/
function isSafeUnusedExportCondition(variable: ScopeVariable): boolean {
if (variable instanceof ESLintScopeVariable) {
return true;
}

Check warning on line 434 in packages/eslint-plugin/src/util/collectUnusedVariables.ts

View check run for this annotation

Codecov / codecov/patch

packages/eslint-plugin/src/util/collectUnusedVariables.ts#L433-L434

Added lines #L433 - L434 were not covered by tests
Copy link
Contributor Author

@nayounsang nayounsang Jun 20, 2025

Choose a reason for hiding this comment

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

need advice

I don't know why this condition is necessary. Can't the arg type of isExported, isMergableExported be used as Variable?
Actually, it is used as Variable only.

if (variable.isTypeVariable && variable.isValueVariable) {
return !variable.defs
.map(d => d.type)
.every(t => exportExceptDefTypes.includes(t));
}
return true;
}

const MERGABLE_TYPES = new Set([
AST_NODE_TYPES.ClassDeclaration,
AST_NODE_TYPES.FunctionDeclaration,
Expand All @@ -426,7 +451,8 @@
* Determine if the variable is directly exported
* @param variable the variable to check
*/
function isMergableExported(variable: ScopeVariable): boolean {
function isMergeableExported(variable: ScopeVariable): boolean {
const safeFlag = isSafeUnusedExportCondition(variable);
// If all of the merged things are of the same type, TS will error if not all of them are exported - so we only need to find one
for (const def of variable.defs) {
// parameters can never be exported.
Expand All @@ -441,7 +467,9 @@
def.node.parent?.type === AST_NODE_TYPES.ExportNamedDeclaration) ||
def.node.parent?.type === AST_NODE_TYPES.ExportDefaultDeclaration
) {
return true;
return (
safeFlag || def.node.type !== AST_NODE_TYPES.TSTypeAliasDeclaration
);
}
}

Expand All @@ -454,6 +482,7 @@
* @returns True if the variable is exported, false if not.
*/
function isExported(variable: ScopeVariable): boolean {
const safeFlag = isSafeUnusedExportCondition(variable);
return variable.defs.some(definition => {
let node = definition.node;

Expand All @@ -465,7 +494,12 @@
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return node.parent!.type.startsWith('Export');
const isExportedFlag = node.parent!.type.startsWith('Export');
Copy link
Contributor Author

@nayounsang nayounsang Jun 20, 2025

Choose a reason for hiding this comment

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

need advice

Is there a reason why didn't use optional chaining before?


return (
isExportedFlag &&
(safeFlag || node.type !== AST_NODE_TYPES.TSTypeAliasDeclaration)
);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1715,6 +1715,24 @@ export {};
],
filename: 'foo.d.ts',
},
// https://github.com/typescript-eslint/typescript-eslint/issues/10658
{
code: `
const A = 0;
export type A = typeof A;
`,
errors: [
{
data: {
action: 'assigned a value',
additional: '',
varName: 'A',
},
line: 2,
messageId: 'usedOnlyAsType',
},
],
},
],

valid: [
Expand Down Expand Up @@ -3018,5 +3036,13 @@ declare class Bar {}
`,
filename: 'foo.d.ts',
},
{
code: `
const A = 0;

type A = typeof A;
export { A };
`,
},
],
});