Skip to content

no-undefined-types treat normal variables as undefined in function scope #1342

Closed
@hecticme

Description

@hecticme

Reproduction: https://stackblitz.com/edit/vitejs-vite-d1gqkb

Expected behavior

I expected that this code snippet does not cause any lint error:

function retrieveHexCode(colorName) {
  const hashmap = {
    white: '#fff',
  };

  // 👇️ Lint error: The type 'hashmap' is undefined.
  /** @typedef {keyof typeof hashmap} KeyOfHashmap */
  // It seems like normal variables are not recognized.
  /** @typedef {typeof hashmap} TypeOfHashmap */

  const colorHexCode = hashmap[/** @type {KeyOfHashmap} */ (colorName)];

  return colorHexCode ?? '#000';
}

const hashmapTwo = {
  white: '#fff',
};

// 👇️ However, the error does not occur outside of function scope.
/** @typedef {typeof hashmapTwo} TypeOfHashmapTwo */

Actual behavior

In the above snippet, hashmap is reported as undefined by jsdoc/no-undefined-types. This only happens inside the function scope, please take a look at the reproduction playground for more details. Intellisense was still working properly.

ESLint Config

import jsdoc from 'eslint-plugin-jsdoc';

export default [
  {
    files: ['**/*.js'],
    plugins: {
      jsdoc,
    },
    rules: {
      'jsdoc/no-undefined-types': [
        'error',
        {
          definedTypes: [],
          disableReporting: false,
          markVariablesAsUsed: true,
        },
      ],
    },
  },
];

ESLint sample

See → https://stackblitz.com/edit/vitejs-vite-d1gqkb or check the code in "Expected behavior".

Environment

  • Node version: v18.20.3
  • ESLint version: v9.15.0
  • eslint-plugin-jsdoc version: 50.5.0

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions