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

fix(eslint-plugin): [no-unnecessary-type-arguments] Use Symbol to check for the same type #4543

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 13 additions & 5 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-arguments.ts
Expand Up @@ -37,7 +37,6 @@ export default util.createRule<[], MessageIds>({
create(context) {
const parserServices = util.getParserServices(context);
const checker = parserServices.program.getTypeChecker();
const sourceCode = context.getSourceCode();

function checkTSArgsAndParameters(
esParameters: TSESTree.TSTypeParameterInstantiation,
Expand All @@ -47,11 +46,20 @@ export default util.createRule<[], MessageIds>({
const i = esParameters.params.length - 1;
const arg = esParameters.params[i];
const param = typeParameters[i];

if (!param?.default) {
return;
}
// TODO: would like checker.areTypesEquivalent. https://github.com/Microsoft/TypeScript/issues/13502
if (
!param?.default ||
param.default.getText() !== sourceCode.getText(arg)
const defaultType = checker.getTypeAtLocation(param.default);
const argTsNode = parserServices.esTreeNodeToTSNodeMap.get(arg);
const argType = checker.getTypeAtLocation(argTsNode);
if (!argType.aliasSymbol && !defaultType.aliasSymbol) {
if (argType.flags !== defaultType.flags) {
return;
}
} else if (
argType.aliasSymbol !== defaultType.aliasSymbol ||
argType.aliasTypeArguments !== defaultType.aliasTypeArguments
) {
return;
}
Expand Down
Expand Up @@ -290,5 +290,32 @@ function bar<T = F<string>>() {}
bar();
`,
},
{
code: `
type DefaultE = { foo: string };
type T<E = DefaultE> = { box: E };
type G = T<DefaultE>;
declare module 'bar' {
type DefaultE = { somethingElse: true };
type G = T<DefaultE>;
}
`,
errors: [
{
line: 4,
column: 12,
messageId: 'unnecessaryTypeParameter',
},
],
output: `
type DefaultE = { foo: string };
type T<E = DefaultE> = { box: E };
type G = T;
declare module 'bar' {
type DefaultE = { somethingElse: true };
type G = T<DefaultE>;
}
`,
},
],
});