eslint-type-tracer is the type tracer for ESLint rules.
Trace and infer types of expression nodes for ESLint rules.
This tool cannot always infer the complete or exact type of an expression. In particular, ESLint analyzes one file at a time, so types that are defined or imported from other files cannot be inferred. As a result, type inference is limited to what can be determined within the current file only.
npm install eslint-type-tracerRequirements
- ESLint v8.57.0 and above
- Node.js v14.18.0, v16.0.0 and above
import { buildTypeTracer } from "eslint-type-tracer";Builds a type tracer function for use in ESLint rules. This function infers the type name of a given AST expression node.
Signature:
function buildTypeTracer(
sourceCode: SourceCode
): (node: TSESTree.Expression) => TypeName[];sourceCode: The ESLintSourceCodeobject.
Returns:
A function that takes an expression node and returns an array of inferred type names (e.g., ["Array"]). If the type cannot be determined, it returns an empty array []。
Example:
const typeTrace = buildTypeTracer(context.sourceCode);
const typeNames = typeTrace(node);
if (typeNames.includes("Array")) {
// node is inferred as Array
}import { buildTypeChecker } from "eslint-type-tracer";Builds a type checker function for use in ESLint rules. This function helps you determine if a given AST node is of a specific type.
Signature:
function buildTypeChecker(
sourceCode: SourceCode,
options?: { aggressive?: boolean }
): (
node: TSESTree.Expression,
className: TypeName,
memberAccessNode?: TSESTree.MemberExpression | TSESTree.Property
) => boolean | "aggressive";sourceCode: The ESLintSourceCodeobject.options.aggressive: If set totrue, returns"aggressive"when the type cannot be determined. Default isfalse.
Returns:
A function that takes an expression node, a type name, and optionally a member access node, and returns true if the node is of the specified type, false if not, or "aggressive" if undetermined and the option is set.
Example:
const typeChecker = buildTypeChecker(context.sourceCode);
if (typeChecker(node, "Array")) {
// node is an Array
}Welcome contributing!
Please use GitHub's Issues/PRs.
npm run testruns tests.
See the LICENSE file for license rights and limitations (MIT).