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

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

Open
hecticme opened this issue Nov 26, 2024 · 0 comments
Open

Comments

@hecticme
Copy link

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant