📄 no-unnecessary-type-arguments¶
📊 Analysis Summary¶
| Metric | Count |
|---|---|
| 🔧 Functions | 12 |
| 📦 Imports | 6 |
| 📑 Type Aliases | 2 |
📚 Table of Contents¶
🛠️ File Location:¶
📂 packages/eslint-plugin/src/rules/no-unnecessary-type-arguments.ts
📤 Default Export¶
| Property | Value |
|---|---|
name |
'no-unnecessary-type-arguments' |
meta.type |
'suggestion' |
meta.docs.description |
'Disallow type arguments that are equal to the default' |
meta.docs.recommended |
'strict' |
meta.docs.requiresTypeChecking |
true |
meta.fixable |
'code' |
meta.messages.unnecessaryTypeParameter |
'This is the default value for this type parameter, so it can be omitted.' |
meta.schema |
[] |
defaultOptions |
[] |
Entry point: create — documented under Functions.
📦 Imports¶
| Name | Source |
|---|---|
TSESTree |
@typescript-eslint/utils |
AST_NODE_TYPES |
@typescript-eslint/utils |
createRule |
../util |
findFirstResult |
../util |
getParserServices |
../util |
isTypeReferenceType |
../util |
Functions¶
create(context: any): { TSTypeParameterInstantiation(node: any): void; }¶
Parameters:
contextany
Returns: { TSTypeParameterInstantiation(node: any): void; }
Calls:
getParserServices (from ../util)services.program.getTypeCheckerisTypeReferenceType (from ../util)checker.getTypeArgumentstypeParameters.atchecker.getTypeAtLocationservices.getTypeAtLocationgetTypeForComparisondefaultTypeResolved.typeArguments.somecontext.reportfixer.removeRangeservices.esTreeNodeToTSNodeMap.getgetTypeParametersFromNodecheckTSArgsAndParameters
Internal Comments:
// Just check the last one. Must specify previous type parameters if the last one is specified. (x2)
// TODO: would like checker.areTypesEquivalent. https://github.com/Microsoft/TypeScript/issues/13502 (x2)
// this check should handle some of the most simple cases of like strings, numbers, etc
// For more complex types (like aliases to generic object types) - TS won't always create a (x2)
// global shared type object for the type - so we need to resort to manually comparing the (x2)
// reference type and the passed type arguments. (x2)
// Also - in case there are aliases - we need to resolve them before we do checks (x2)
// ensure the resolved type AND all the parameters are the same (x5)
// TypeScript does not apply default type parameters in instantiation
// expressions, so explicit type args here are always meaningful.
Code
create(context) {
const services = getParserServices(context);
const checker = services.program.getTypeChecker();
function getTypeForComparison(type: ts.Type): {
type: ts.Type;
typeArguments: readonly ts.Type[];
} {
if (isTypeReferenceType(type)) {
return {
type: type.target,
typeArguments: checker.getTypeArguments(type),
};
}
return {
type,
typeArguments: [],
};
}
function checkTSArgsAndParameters(
esParameters: TSESTree.TSTypeParameterInstantiation,
typeParameters: readonly ts.TypeParameterDeclaration[],
): void {
// Just check the last one. Must specify previous type parameters if the last one is specified.
const i = esParameters.params.length - 1;
const arg = esParameters.params[i];
const param = typeParameters.at(i);
if (!param?.default) {
return;
}
// TODO: would like checker.areTypesEquivalent. https://github.com/Microsoft/TypeScript/issues/13502
const defaultType = checker.getTypeAtLocation(param.default);
const argType = services.getTypeAtLocation(arg);
// this check should handle some of the most simple cases of like strings, numbers, etc
if (defaultType !== argType) {
// For more complex types (like aliases to generic object types) - TS won't always create a
// global shared type object for the type - so we need to resort to manually comparing the
// reference type and the passed type arguments.
// Also - in case there are aliases - we need to resolve them before we do checks
const defaultTypeResolved = getTypeForComparison(defaultType);
const argTypeResolved = getTypeForComparison(argType);
if (
// ensure the resolved type AND all the parameters are the same
defaultTypeResolved.type !== argTypeResolved.type ||
defaultTypeResolved.typeArguments.length !==
argTypeResolved.typeArguments.length ||
defaultTypeResolved.typeArguments.some(
(t, i) => t !== argTypeResolved.typeArguments[i],
)
) {
return;
}
}
context.report({
node: arg,
messageId: 'unnecessaryTypeParameter',
fix: fixer =>
fixer.removeRange(
i === 0
? esParameters.range
: [esParameters.params[i - 1].range[1], arg.range[1]],
),
});
}
return {
TSTypeParameterInstantiation(node): void {
// TypeScript does not apply default type parameters in instantiation
// expressions, so explicit type args here are always meaningful.
if (node.parent.type === AST_NODE_TYPES.TSInstantiationExpression) {
return;
}
const expression = services.esTreeNodeToTSNodeMap.get(node);
const typeParameters = getTypeParametersFromNode(
node,
expression,
checker,
);
if (typeParameters) {
checkTSArgsAndParameters(node, typeParameters);
}
},
};
}
getTypeParametersFromNode(node: TSESTree.TSTypeParameterInstantiation, tsNode: ParameterCapableTSNode, checker: ts.TypeChecker): readonly ts.TypeParameterDeclaration[] | undefined¶
Parameters:
nodeTSESTree.TSTypeParameterInstantiationtsNodeParameterCapableTSNodecheckerts.TypeChecker
Returns: readonly ts.TypeParameterDeclaration[] | undefined
Calls:
ts.isExpressionWithTypeArgumentsgetTypeParametersFromTypets.isTypeReferenceNodets.isCallExpressionts.isNewExpressionts.isTaggedTemplateExpressionts.isJsxOpeningElementts.isJsxSelfClosingElementgetTypeParametersFromCall
Code
function getTypeParametersFromNode(
node: TSESTree.TSTypeParameterInstantiation,
tsNode: ParameterCapableTSNode,
checker: ts.TypeChecker,
): readonly ts.TypeParameterDeclaration[] | undefined {
if (ts.isExpressionWithTypeArguments(tsNode)) {
return getTypeParametersFromType(node, tsNode.expression, checker);
}
if (ts.isTypeReferenceNode(tsNode)) {
return getTypeParametersFromType(node, tsNode.typeName, checker);
}
if (
ts.isCallExpression(tsNode) ||
ts.isNewExpression(tsNode) ||
ts.isTaggedTemplateExpression(tsNode) ||
ts.isJsxOpeningElement(tsNode) ||
ts.isJsxSelfClosingElement(tsNode)
) {
return getTypeParametersFromCall(node, tsNode, checker);
}
return undefined;
}
getTypeParametersFromType(…): readonly ts.TypeParameterDeclaration[] | undefined¶
Parameters:
nodeTSESTree.TSTypeParameterInstantiationtypets.ClassDeclaration | ts.EntityName | ts.Expressioncheckerts.TypeChecker
Returns: readonly ts.TypeParameterDeclaration[] | undefined
Calls:
checker.getSymbolAtLocationgetAliasedSymbolsym.getDeclarationssortDeclarationsByTypeValueContextfindFirstResult (from ../util)ts.isTypeAliasDeclarationts.isInterfaceDeclarationts.isClassLikets.isVariableDeclarationgetConstructSignatureDeclaration
Code
function getTypeParametersFromType(
node: TSESTree.TSTypeParameterInstantiation,
type: ts.ClassDeclaration | ts.EntityName | ts.Expression,
checker: ts.TypeChecker,
): readonly ts.TypeParameterDeclaration[] | undefined {
const symAtLocation = checker.getSymbolAtLocation(type);
if (!symAtLocation) {
return undefined;
}
const sym = getAliasedSymbol(symAtLocation, checker);
const declarations = sym.getDeclarations();
if (!declarations) {
return undefined;
}
const sortedDeclarations = sortDeclarationsByTypeValueContext(
node,
declarations,
);
return findFirstResult(sortedDeclarations, decl => {
if (
ts.isTypeAliasDeclaration(decl) ||
ts.isInterfaceDeclaration(decl) ||
ts.isClassLike(decl)
) {
return decl.typeParameters;
}
if (ts.isVariableDeclaration(decl)) {
return getConstructSignatureDeclaration(symAtLocation, checker)
?.typeParameters;
}
return undefined;
});
}
getTypeParametersFromCall(…): readonly ts.TypeParameterDeclaration[] | undefined¶
Parameters:
nodeTSESTree.TSTypeParameterInstantiationtsNode| ts.CallExpression | ts.JsxOpeningElement | ts.JsxSelfClosingElement | ts.NewExpression | ts.TaggedTemplateExpressioncheckerts.TypeChecker
Returns: readonly ts.TypeParameterDeclaration[] | undefined
Calls:
checker.getResolvedSignaturesig?.getDeclarationts.isNewExpressiongetTypeParametersFromType
Code
function getTypeParametersFromCall(
node: TSESTree.TSTypeParameterInstantiation,
tsNode:
| ts.CallExpression
| ts.JsxOpeningElement
| ts.JsxSelfClosingElement
| ts.NewExpression
| ts.TaggedTemplateExpression,
checker: ts.TypeChecker,
): readonly ts.TypeParameterDeclaration[] | undefined {
const sig = checker.getResolvedSignature(tsNode);
const sigDecl = sig?.getDeclaration();
if (!sigDecl) {
return ts.isNewExpression(tsNode)
? getTypeParametersFromType(node, tsNode.expression, checker)
: undefined;
}
return sigDecl.typeParameters;
}
getAliasedSymbol(symbol: ts.Symbol, checker: ts.TypeChecker): ts.Symbol¶
Parameters:
symbolts.Symbolcheckerts.TypeChecker
Returns: ts.Symbol
Calls:
tsutils.isSymbolFlagSetchecker.getAliasedSymbol
Code
isInTypeContext(node: TSESTree.TSTypeParameterInstantiation): boolean¶
Parameters:
nodeTSESTree.TSTypeParameterInstantiation
Returns: boolean
Code
isTypeContextDeclaration(decl: ts.Declaration): any¶
Parameters:
declts.Declaration
Returns: any
Calls:
ts.isTypeAliasDeclarationts.isInterfaceDeclaration
Code
typeFirstCompare(declA: ts.Declaration, declB: ts.Declaration): number¶
Parameters:
declAts.DeclarationdeclBts.Declaration
Returns: number
Calls:
isTypeContextDeclarationNumber
Code
sortDeclarationsByTypeValueContext(node: TSESTree.TSTypeParameterInstantiation, declarations: ts.Declaration[]): ts.Declaration[]¶
Parameters:
nodeTSESTree.TSTypeParameterInstantiationdeclarationsts.Declaration[]
Returns: ts.Declaration[]
Calls:
[...declarations].sortisInTypeContextsorted.reverse
Code
getConstructSignatureDeclaration(symbol: ts.Symbol, checker: ts.TypeChecker): ts.SignatureDeclaration | undefined¶
Parameters:
symbolts.Symbolcheckerts.TypeChecker
Returns: ts.SignatureDeclaration | undefined
Calls:
checker.getTypeOfSymboltype.getConstructSignaturessig.at(0)?.getDeclaration
Code
Internal helpers¶
Declared inside another function in this file.
getTypeForComparison(type: ts.Type): { type: ts.Type; typeArguments: readonly ts.Type[]; }¶
Parameters:
typets.Type
Returns: {
type: ts.Type;
typeArguments: readonly ts.Type[];
}
Calls:
isTypeReferenceType (from ../util)checker.getTypeArguments
Code
checkTSArgsAndParameters(esParameters: TSESTree.TSTypeParameterInstantiation, typeParameters: readonly ts.TypeParameterDeclaration[]): void¶
Parameters:
esParametersTSESTree.TSTypeParameterInstantiationtypeParametersreadonly ts.TypeParameterDeclaration[]
Returns: void
Calls:
typeParameters.atchecker.getTypeAtLocationservices.getTypeAtLocationgetTypeForComparisondefaultTypeResolved.typeArguments.somecontext.reportfixer.removeRange
Internal Comments:
// Just check the last one. Must specify previous type parameters if the last one is specified. (x2)
// TODO: would like checker.areTypesEquivalent. https://github.com/Microsoft/TypeScript/issues/13502 (x2)
// this check should handle some of the most simple cases of like strings, numbers, etc
// For more complex types (like aliases to generic object types) - TS won't always create a (x2)
// global shared type object for the type - so we need to resort to manually comparing the (x2)
// reference type and the passed type arguments. (x2)
// Also - in case there are aliases - we need to resolve them before we do checks (x2)
// ensure the resolved type AND all the parameters are the same (x5)
Code
function checkTSArgsAndParameters(
esParameters: TSESTree.TSTypeParameterInstantiation,
typeParameters: readonly ts.TypeParameterDeclaration[],
): void {
// Just check the last one. Must specify previous type parameters if the last one is specified.
const i = esParameters.params.length - 1;
const arg = esParameters.params[i];
const param = typeParameters.at(i);
if (!param?.default) {
return;
}
// TODO: would like checker.areTypesEquivalent. https://github.com/Microsoft/TypeScript/issues/13502
const defaultType = checker.getTypeAtLocation(param.default);
const argType = services.getTypeAtLocation(arg);
// this check should handle some of the most simple cases of like strings, numbers, etc
if (defaultType !== argType) {
// For more complex types (like aliases to generic object types) - TS won't always create a
// global shared type object for the type - so we need to resort to manually comparing the
// reference type and the passed type arguments.
// Also - in case there are aliases - we need to resolve them before we do checks
const defaultTypeResolved = getTypeForComparison(defaultType);
const argTypeResolved = getTypeForComparison(argType);
if (
// ensure the resolved type AND all the parameters are the same
defaultTypeResolved.type !== argTypeResolved.type ||
defaultTypeResolved.typeArguments.length !==
argTypeResolved.typeArguments.length ||
defaultTypeResolved.typeArguments.some(
(t, i) => t !== argTypeResolved.typeArguments[i],
)
) {
return;
}
}
context.report({
node: arg,
messageId: 'unnecessaryTypeParameter',
fix: fixer =>
fixer.removeRange(
i === 0
? esParameters.range
: [esParameters.params[i - 1].range[1], arg.range[1]],
),
});
}
Type Aliases¶
ParameterCapableTSNode¶
type ParameterCapableTSNode = | ts.CallExpression
| ts.ExpressionWithTypeArguments
| ts.ImportTypeNode
| ts.JsxOpeningElement
| ts.JsxSelfClosingElement
| ts.NewExpression
| ts.TaggedTemplateExpression
| ts.TypeQueryNode
| ts.TypeReferenceNode;
MessageIds¶
Generated by Syntax Scribe