Skip to content

Add a TypeScript config for mixed TS/JS projects #1101

Open
@jaydenseric

Description

@jaydenseric

Motivation

At the moment, there isn’t a config for a TypeScript project that has type checking in the TypeScript modules (.ts, .tsx, .mts, .cts) as well as the JavaScript modules (.js, .mjs, .cjs). Quite often you will have a src/ dir that contains all the .mts files for TypeScript to compile to dist/, but you also have files like .eslintrc.js that don't contain TypeScript syntax but still are type checked by TypeScript via the // @ts-check comment and TypeScript flavour of JSDoc comments.

It would be good to add a eslint-plugin-jsdoc recommended config for TypeScript mixed TS/JS projects, deprecating the current recommended TypeScript configs that assume a TypeScript type checked project is only TS, or only JS, but not mixed.

Current behavior

To try to get things working with v44, here was my approach in .eslintrc.js (with all non eslint-plugin-jsdoc config removed for brevity):

// @ts-check

/** @type {import("eslint").Linter.Config} */
module.exports = {
  root: true,
  extends: [
    "plugin:jsdoc/recommended-typescript-error",
  ],
  rules: {
    // Only TypeScript should check JSDoc types, see:
    // https://github.com/gajus/eslint-plugin-jsdoc/issues/888#issuecomment-1544914446
    "jsdoc/no-undefined-types": "off",
  },
  overrides: [
    {
      // ESM and CJS JavaScript modules.
      files: ["*.mjs", "*.cjs", "*.js"],
      rules: {
        "jsdoc/check-tag-names": [
          "error",
          {
            // Allow type related JSDoc tags like `@type`, as TypeScript syntax
            // is not available in these JavaScript modules.
            typed: false,
          },
        ],
        // Allow types in JSDoc tags like `@param`, as TypeScript syntax is not
        // available in these JavaScript modules.
        "jsdoc/no-types": "off",
      },
    },
  ],
};

This approach of picking a config and then trying to overwrite it with overrides is frustrating to figure out and maintain, and most people don't understand how all this fits together well enough to get it right.

Desired behavior

There would be a recommended config that targets the specific types of JavaScript and TypeScript modules that can exist in a project by file extension, applying only the relevant JSDoc rules to each kind of module.

Alternatives considered

  • Status quo, documenting suggested overrides for different module kinds.
  • Being even smarter and have rules like jsdoc/no-undefined-types become no-ops if they individually detect if the current module is TypeScript (.ts, .tsx, .mts, .cts), or if the current JavaScript module falls under TS config for checking all JavaScript modules, or if the JavaScript module contains a // @ts-check comment. You could actually have just one eslint-plugin-jsdoc recommended config then, that would universally work for all projects regardless if they are type-checked with TypeScript.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions