Skip to content

no-unnecessary-type-assertion: False positive on array-to-tuple assertion #1756

Description

@ehoogeveen-medweb

Typing a JavaScript array of literals with that type turns it into a tuple - a poor man's replacement for TypeScript's as const:

const arr = ['foo'];

has type string[],

const arr = /** @type {['foo']} */ (['foo']);

has type ['foo'].

While looking into this I learned that since TypeScript version 4.5, you can actually do this instead:

const arr = /** @type {const} */ (['foo']);

which is much nicer. But the old way is not an unnecessary type assertion.

Expected behavior

No warning or give a more specific warning and autofix to /** @type {const} */.

Actual behavior

The rule reports the type assertion as unnecessary and the autofix removes it, changing the type.

ESLint Config

import { jsdoc } from 'eslint-plugin-jsdoc';

export default [
  jsdoc({
    rules: {
      'jsdoc/no-unnecessary-type-assertion': 'error',
    },
  }),
];

ESLint sample

const arr = /** @type {['foo']} */ (['foo']);

Environment

  • Node version: 24.20.0
  • ESLint version: 10.9.1
  • eslint-plugin-jsdoc version: 64.3.3

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions