Skip to content

⬅️ Back to Table of Contents

📄 no-explicit-any

📊 Analysis Summary

Metric Count
🔧 Functions 13
📦 Imports 4
📑 Type Aliases 2

📚 Table of Contents

🛠️ File Location:

📂 packages/eslint-plugin/src/rules/no-explicit-any.ts

📤 Default Export

export default createRule<Options, MessageIds>({ ... })
Property Value
name 'no-explicit-any'
meta.type 'suggestion'
meta.docs.description 'Disallow the any type'
meta.docs.recommended 'recommended'
meta.fixable 'code'
meta.hasSuggestions true
meta.messages.suggestNever "Use never instead, this is useful when instantiating generic type parameters that you don't need to know the type ...
meta.messages.suggestPropertyKey 'Use PropertyKey instead, this is more explicit than keyof any.'
meta.messages.suggestUnknown 'Use unknown instead, this will force you to explicitly, and safely assert the type is correct.'
meta.messages.unexpectedAny 'Unexpected any. Specify a different type.'
meta.schema [ { type: 'object', additionalProperties: false, properties: { fixToUnknown: { type: 'boolean', description: 'Whether...
defaultOptions [ { fixToUnknown: false, ignoreRestArgs: false, }, ]

Entry point: create — documented under Functions.


📦 Imports

Name Source
TSESLint @typescript-eslint/utils
TSESTree @typescript-eslint/utils
AST_NODE_TYPES @typescript-eslint/utils
createRule ../util

Functions

create(context: any, [{ fixToUnknown, ignoreRestArgs…: any): { TSAnyKeyword(node: any): void; }

Parameters:

  • context any
  • [{ fixToUnknown, ignoreRestArgs }] any

Returns: { TSAnyKeyword(node: any): void; }

Calls:

  • [ AST_NODE_TYPES.ArrowFunctionExpression, // const x = (...args: any[]) => {}; AST_NODE_TYPES.FunctionDeclaration, // function f(...args: any[]) {} AST_NODE_TYPES.FunctionExpression, // const x = function(...args: any[]) {}; AST_NODE_TYPES.TSCallSignatureDeclaration, // type T = {(...args: any[]): unknown}; AST_NODE_TYPES.TSConstructorType, // type T = new (...args: any[]) => unknown AST_NODE_TYPES.TSConstructSignatureDeclaration, // type T = {new (...args: any[]): unknown}; AST_NODE_TYPES.TSDeclareFunction, // declare function _8(...args: any[]): unknown; AST_NODE_TYPES.TSEmptyBodyFunctionExpression, // declare class A { f(...args: any[]): unknown; } AST_NODE_TYPES.TSFunctionType, // type T = (...args: any[]) => unknown; AST_NODE_TYPES.TSMethodSignature, // type T = {f(...args: any[]): unknown}; ].includes
  • isNodeValidFunction
  • ['Array', 'ReadonlyArray'].includes
  • isNodeReadonlyTSTypeOperator
  • isNodeValidArrayTSTypeReference
  • isNodeRestElementInFunction
  • isNodeValidTSType
  • isGreatGrandparentRestElement
  • isGreatGreatGrandparentRestElement
  • fixer.replaceText
  • isNodeWithinKeyofAny
  • isNodeDescendantOfRestElementInFunction
  • createPropertyKeyFixer
  • context.report

Internal Comments:

/**
     * Checks if the node is an arrow function, function/constructor declaration or function expression
     * @param node the node to be validated.
     * @returns true if the node is any kind of function declaration or expression
     * @private
     */
/**
     * Checks if the node is a rest element child node of a function
     * @param node the node to be validated.
     * @returns true if the node is a rest element child node of a function
     * @private
     */
/**
     * Checks if the node is a TSTypeOperator node with a readonly operator
     * @param node the node to be validated.
     * @returns true if the node is a TSTypeOperator node with a readonly operator
     * @private
     */
/**
     * Checks if the node is a TSTypeReference node with an Array identifier
     * @param node the node to be validated.
     * @returns true if the node is a TSTypeReference node with an Array identifier
     * @private
     */
/**
     * Checks if the node is a valid TSTypeOperator or TSTypeReference node
     * @param node the node to be validated.
     * @returns true if the node is a valid TSTypeOperator or TSTypeReference node
     * @private
     */
/**
     * Checks if the great grand-parent node is a RestElement node in a function
     * @param node the node to be validated.
     * @returns true if the great grand-parent node is a RestElement node in a function
     * @private
     */
/**
     * Checks if the great great grand-parent node is a valid RestElement node in a function
     * @param node the node to be validated.
     * @returns true if the great great grand-parent node is a valid RestElement node in a function
     * @private
     */
/**
     * Checks if the great grand-parent or the great great grand-parent node is a RestElement node
     * @param node the node to be validated.
     * @returns true if the great grand-parent or the great great grand-parent node is a RestElement node
     * @private
     */
/**
     * Checks if the node is within a keyof any expression
     * @param node the node to be validated.
     * @returns true if the node is within a keyof any expression, false otherwise
     * @private
     */
/**
     * Creates a fixer that replaces a keyof any with PropertyKey
     * @param node the node to be fixed.
     * @returns a function that will fix the node.
     * @private
     */

Code
create(context, [{ fixToUnknown, ignoreRestArgs }]) {
    /**
     * Checks if the node is an arrow function, function/constructor declaration or function expression
     * @param node the node to be validated.
     * @returns true if the node is any kind of function declaration or expression
     * @private
     */
    function isNodeValidFunction(node: TSESTree.Node): boolean {
      return [
        AST_NODE_TYPES.ArrowFunctionExpression, // const x = (...args: any[]) => {};
        AST_NODE_TYPES.FunctionDeclaration, // function f(...args: any[]) {}
        AST_NODE_TYPES.FunctionExpression, // const x = function(...args: any[]) {};
        AST_NODE_TYPES.TSCallSignatureDeclaration, // type T = {(...args: any[]): unknown};
        AST_NODE_TYPES.TSConstructorType, // type T = new (...args: any[]) => unknown
        AST_NODE_TYPES.TSConstructSignatureDeclaration, // type T = {new (...args: any[]): unknown};
        AST_NODE_TYPES.TSDeclareFunction, // declare function _8(...args: any[]): unknown;
        AST_NODE_TYPES.TSEmptyBodyFunctionExpression, // declare class A { f(...args: any[]): unknown; }
        AST_NODE_TYPES.TSFunctionType, // type T = (...args: any[]) => unknown;
        AST_NODE_TYPES.TSMethodSignature, // type T = {f(...args: any[]): unknown};
      ].includes(node.type);
    }

    /**
     * Checks if the node is a rest element child node of a function
     * @param node the node to be validated.
     * @returns true if the node is a rest element child node of a function
     * @private
     */
    function isNodeRestElementInFunction(node: TSESTree.Node): boolean {
      return (
        node.type === AST_NODE_TYPES.RestElement &&
        isNodeValidFunction(node.parent)
      );
    }

    /**
     * Checks if the node is a TSTypeOperator node with a readonly operator
     * @param node the node to be validated.
     * @returns true if the node is a TSTypeOperator node with a readonly operator
     * @private
     */
    function isNodeReadonlyTSTypeOperator(node: TSESTree.Node): boolean {
      return (
        node.type === AST_NODE_TYPES.TSTypeOperator &&
        node.operator === 'readonly'
      );
    }

    /**
     * Checks if the node is a TSTypeReference node with an Array identifier
     * @param node the node to be validated.
     * @returns true if the node is a TSTypeReference node with an Array identifier
     * @private
     */
    function isNodeValidArrayTSTypeReference(node: TSESTree.Node): boolean {
      return (
        node.type === AST_NODE_TYPES.TSTypeReference &&
        node.typeName.type === AST_NODE_TYPES.Identifier &&
        ['Array', 'ReadonlyArray'].includes(node.typeName.name)
      );
    }

    /**
     * Checks if the node is a valid TSTypeOperator or TSTypeReference node
     * @param node the node to be validated.
     * @returns true if the node is a valid TSTypeOperator or TSTypeReference node
     * @private
     */
    function isNodeValidTSType(node: TSESTree.Node): boolean {
      return (
        isNodeReadonlyTSTypeOperator(node) ||
        isNodeValidArrayTSTypeReference(node)
      );
    }

    /**
     * Checks if the great grand-parent node is a RestElement node in a function
     * @param node the node to be validated.
     * @returns true if the great grand-parent node is a RestElement node in a function
     * @private
     */
    function isGreatGrandparentRestElement(node: TSESTree.Node): boolean {
      return (
        node.parent?.parent?.parent != null &&
        isNodeRestElementInFunction(node.parent.parent.parent)
      );
    }

    /**
     * Checks if the great great grand-parent node is a valid RestElement node in a function
     * @param node the node to be validated.
     * @returns true if the great great grand-parent node is a valid RestElement node in a function
     * @private
     */
    function isGreatGreatGrandparentRestElement(node: TSESTree.Node): boolean {
      return (
        node.parent?.parent?.parent?.parent != null &&
        isNodeValidTSType(node.parent.parent) &&
        isNodeRestElementInFunction(node.parent.parent.parent.parent)
      );
    }

    /**
     * Checks if the great grand-parent or the great great grand-parent node is a RestElement node
     * @param node the node to be validated.
     * @returns true if the great grand-parent or the great great grand-parent node is a RestElement node
     * @private
     */
    function isNodeDescendantOfRestElementInFunction(
      node: TSESTree.Node,
    ): boolean {
      return (
        isGreatGrandparentRestElement(node) ||
        isGreatGreatGrandparentRestElement(node)
      );
    }

    /**
     * Checks if the node is within a keyof any expression
     * @param node the node to be validated.
     * @returns true if the node is within a keyof any expression, false otherwise
     * @private
     */
    function isNodeWithinKeyofAny(node: TSESTree.TSAnyKeyword): boolean {
      return (
        node.parent.type === AST_NODE_TYPES.TSTypeOperator &&
        node.parent.operator === 'keyof'
      );
    }

    /**
     * Creates a fixer that replaces a keyof any with PropertyKey
     * @param node the node to be fixed.
     * @returns a function that will fix the node.
     * @private
     */
    function createPropertyKeyFixer(node: TSESTree.TSAnyKeyword) {
      return (fixer: TSESLint.RuleFixer) => {
        return fixer.replaceText(node.parent, 'PropertyKey');
      };
    }

    return {
      TSAnyKeyword(node): void {
        const isKeyofAny = isNodeWithinKeyofAny(node);

        if (ignoreRestArgs && isNodeDescendantOfRestElementInFunction(node)) {
          return;
        }

        const fixOrSuggest: {
          fix: TSESLint.ReportFixFunction | null;
          suggest: TSESLint.ReportSuggestionArray<MessageIds> | null;
        } = {
          fix: null,
          suggest: isKeyofAny
            ? [
                {
                  messageId: 'suggestPropertyKey',
                  fix: createPropertyKeyFixer(node),
                },
              ]
            : [
                {
                  messageId: 'suggestUnknown',
                  fix: fixer => fixer.replaceText(node, 'unknown'),
                },
                {
                  messageId: 'suggestNever',
                  fix: fixer => fixer.replaceText(node, 'never'),
                },
              ],
        };

        if (fixToUnknown) {
          fixOrSuggest.fix = isKeyofAny
            ? createPropertyKeyFixer(node)
            : fixer => fixer.replaceText(node, 'unknown');
        }

        context.report({
          node,
          messageId: 'unexpectedAny',
          ...fixOrSuggest,
        });
      },
    };
  }

Internal helpers

Declared inside another function in this file.

isNodeValidFunction(node: TSESTree.Node): boolean

Checks if the node is an arrow function, function/constructor declaration or function expression

Parameters:

  • node any: the node to be validated.

Returns: undefined true if the node is any kind of function declaration or expression

Tags: @private

Raw JSDoc
/**
     * Checks if the node is an arrow function, function/constructor declaration or function expression
     * @param node the node to be validated.
     * @returns true if the node is any kind of function declaration or expression
     * @private
     */

Calls:

  • [ AST_NODE_TYPES.ArrowFunctionExpression, // const x = (...args: any[]) => {}; AST_NODE_TYPES.FunctionDeclaration, // function f(...args: any[]) {} AST_NODE_TYPES.FunctionExpression, // const x = function(...args: any[]) {}; AST_NODE_TYPES.TSCallSignatureDeclaration, // type T = {(...args: any[]): unknown}; AST_NODE_TYPES.TSConstructorType, // type T = new (...args: any[]) => unknown AST_NODE_TYPES.TSConstructSignatureDeclaration, // type T = {new (...args: any[]): unknown}; AST_NODE_TYPES.TSDeclareFunction, // declare function _8(...args: any[]): unknown; AST_NODE_TYPES.TSEmptyBodyFunctionExpression, // declare class A { f(...args: any[]): unknown; } AST_NODE_TYPES.TSFunctionType, // type T = (...args: any[]) => unknown; AST_NODE_TYPES.TSMethodSignature, // type T = {f(...args: any[]): unknown}; ].includes
Code
function isNodeValidFunction(node: TSESTree.Node): boolean {
      return [
        AST_NODE_TYPES.ArrowFunctionExpression, // const x = (...args: any[]) => {};
        AST_NODE_TYPES.FunctionDeclaration, // function f(...args: any[]) {}
        AST_NODE_TYPES.FunctionExpression, // const x = function(...args: any[]) {};
        AST_NODE_TYPES.TSCallSignatureDeclaration, // type T = {(...args: any[]): unknown};
        AST_NODE_TYPES.TSConstructorType, // type T = new (...args: any[]) => unknown
        AST_NODE_TYPES.TSConstructSignatureDeclaration, // type T = {new (...args: any[]): unknown};
        AST_NODE_TYPES.TSDeclareFunction, // declare function _8(...args: any[]): unknown;
        AST_NODE_TYPES.TSEmptyBodyFunctionExpression, // declare class A { f(...args: any[]): unknown; }
        AST_NODE_TYPES.TSFunctionType, // type T = (...args: any[]) => unknown;
        AST_NODE_TYPES.TSMethodSignature, // type T = {f(...args: any[]): unknown};
      ].includes(node.type);
    }

isNodeRestElementInFunction(node: TSESTree.Node): boolean

Checks if the node is a rest element child node of a function

Parameters:

  • node any: the node to be validated.

Returns: undefined true if the node is a rest element child node of a function

Tags: @private

Raw JSDoc
/**
     * Checks if the node is a rest element child node of a function
     * @param node the node to be validated.
     * @returns true if the node is a rest element child node of a function
     * @private
     */

Calls:

  • isNodeValidFunction
Code
function isNodeRestElementInFunction(node: TSESTree.Node): boolean {
      return (
        node.type === AST_NODE_TYPES.RestElement &&
        isNodeValidFunction(node.parent)
      );
    }

isNodeReadonlyTSTypeOperator(node: TSESTree.Node): boolean

Checks if the node is a TSTypeOperator node with a readonly operator

Parameters:

  • node any: the node to be validated.

Returns: undefined true if the node is a TSTypeOperator node with a readonly operator

Tags: @private

Raw JSDoc
/**
     * Checks if the node is a TSTypeOperator node with a readonly operator
     * @param node the node to be validated.
     * @returns true if the node is a TSTypeOperator node with a readonly operator
     * @private
     */
Code
function isNodeReadonlyTSTypeOperator(node: TSESTree.Node): boolean {
      return (
        node.type === AST_NODE_TYPES.TSTypeOperator &&
        node.operator === 'readonly'
      );
    }

isNodeValidArrayTSTypeReference(node: TSESTree.Node): boolean

Checks if the node is a TSTypeReference node with an Array identifier

Parameters:

  • node any: the node to be validated.

Returns: undefined true if the node is a TSTypeReference node with an Array identifier

Tags: @private

Raw JSDoc
/**
     * Checks if the node is a TSTypeReference node with an Array identifier
     * @param node the node to be validated.
     * @returns true if the node is a TSTypeReference node with an Array identifier
     * @private
     */

Calls:

  • ['Array', 'ReadonlyArray'].includes
Code
function isNodeValidArrayTSTypeReference(node: TSESTree.Node): boolean {
      return (
        node.type === AST_NODE_TYPES.TSTypeReference &&
        node.typeName.type === AST_NODE_TYPES.Identifier &&
        ['Array', 'ReadonlyArray'].includes(node.typeName.name)
      );
    }

isNodeValidTSType(node: TSESTree.Node): boolean

Checks if the node is a valid TSTypeOperator or TSTypeReference node

Parameters:

  • node any: the node to be validated.

Returns: undefined true if the node is a valid TSTypeOperator or TSTypeReference node

Tags: @private

Raw JSDoc
/**
     * Checks if the node is a valid TSTypeOperator or TSTypeReference node
     * @param node the node to be validated.
     * @returns true if the node is a valid TSTypeOperator or TSTypeReference node
     * @private
     */

Calls:

  • isNodeReadonlyTSTypeOperator
  • isNodeValidArrayTSTypeReference
Code
function isNodeValidTSType(node: TSESTree.Node): boolean {
      return (
        isNodeReadonlyTSTypeOperator(node) ||
        isNodeValidArrayTSTypeReference(node)
      );
    }

isGreatGrandparentRestElement(node: TSESTree.Node): boolean

Checks if the great grand-parent node is a RestElement node in a function

Parameters:

  • node any: the node to be validated.

Returns: undefined true if the great grand-parent node is a RestElement node in a function

Tags: @private

Raw JSDoc
/**
     * Checks if the great grand-parent node is a RestElement node in a function
     * @param node the node to be validated.
     * @returns true if the great grand-parent node is a RestElement node in a function
     * @private
     */

Calls:

  • isNodeRestElementInFunction
Code
function isGreatGrandparentRestElement(node: TSESTree.Node): boolean {
      return (
        node.parent?.parent?.parent != null &&
        isNodeRestElementInFunction(node.parent.parent.parent)
      );
    }

isGreatGreatGrandparentRestElement(node: TSESTree.Node): boolean

Checks if the great great grand-parent node is a valid RestElement node in a function

Parameters:

  • node any: the node to be validated.

Returns: undefined true if the great great grand-parent node is a valid RestElement node in a function

Tags: @private

Raw JSDoc
/**
     * Checks if the great great grand-parent node is a valid RestElement node in a function
     * @param node the node to be validated.
     * @returns true if the great great grand-parent node is a valid RestElement node in a function
     * @private
     */

Calls:

  • isNodeValidTSType
  • isNodeRestElementInFunction
Code
function isGreatGreatGrandparentRestElement(node: TSESTree.Node): boolean {
      return (
        node.parent?.parent?.parent?.parent != null &&
        isNodeValidTSType(node.parent.parent) &&
        isNodeRestElementInFunction(node.parent.parent.parent.parent)
      );
    }

isNodeDescendantOfRestElementInFunction(node: TSESTree.Node): boolean

Checks if the great grand-parent or the great great grand-parent node is a RestElement node

Parameters:

  • node any: the node to be validated.

Returns: undefined true if the great grand-parent or the great great grand-parent node is a RestElement node

Tags: @private

Raw JSDoc
/**
     * Checks if the great grand-parent or the great great grand-parent node is a RestElement node
     * @param node the node to be validated.
     * @returns true if the great grand-parent or the great great grand-parent node is a RestElement node
     * @private
     */

Calls:

  • isGreatGrandparentRestElement
  • isGreatGreatGrandparentRestElement
Code
function isNodeDescendantOfRestElementInFunction(
      node: TSESTree.Node,
    ): boolean {
      return (
        isGreatGrandparentRestElement(node) ||
        isGreatGreatGrandparentRestElement(node)
      );
    }

isNodeWithinKeyofAny(node: TSESTree.TSAnyKeyword): boolean

Checks if the node is within a keyof any expression

Parameters:

  • node any: the node to be validated.

Returns: undefined true if the node is within a keyof any expression, false otherwise

Tags: @private

Raw JSDoc
/**
     * Checks if the node is within a keyof any expression
     * @param node the node to be validated.
     * @returns true if the node is within a keyof any expression, false otherwise
     * @private
     */
Code
function isNodeWithinKeyofAny(node: TSESTree.TSAnyKeyword): boolean {
      return (
        node.parent.type === AST_NODE_TYPES.TSTypeOperator &&
        node.parent.operator === 'keyof'
      );
    }

createPropertyKeyFixer(node: TSESTree.TSAnyKeyword): (fixer: TSESLint.RuleFixer) => any

Creates a fixer that replaces a keyof any with PropertyKey

Parameters:

  • node any: the node to be fixed.

Returns: undefined a function that will fix the node.

Tags: @private

Raw JSDoc
/**
     * Creates a fixer that replaces a keyof any with PropertyKey
     * @param node the node to be fixed.
     * @returns a function that will fix the node.
     * @private
     */

Calls:

  • fixer.replaceText
Code
function createPropertyKeyFixer(node: TSESTree.TSAnyKeyword) {
      return (fixer: TSESLint.RuleFixer) => {
        return fixer.replaceText(node.parent, 'PropertyKey');
      };
    }

suggest.fix(fixer: any): any

Parameters:

  • fixer any

Returns: any

Calls:

  • fixer.replaceText
Code
fixer => fixer.replaceText(node, 'unknown')

suggest.fix(fixer: any): any

Parameters:

  • fixer any

Returns: any

Calls:

  • fixer.replaceText
Code
fixer => fixer.replaceText(node, 'never')

Type Aliases

Options

type Options = [
  {
    fixToUnknown?: boolean;
    ignoreRestArgs?: boolean;
  },
];

MessageIds

type MessageIds = 'suggestNever' | 'suggestPropertyKey' | 'suggestUnknown' | 'unexpectedAny';

Generated by Syntax Scribe