Skip to content

Option to disable JsDoc parsing #23708

@nickserv

Description

@nickserv

A description of the problem you're trying to solve

JsDoc comment parsing is very useful when typechecking JavaScript files with allowJs and checkJs, but sometimes it gets in the way of migrating existing JsDoc files to TypeScript, especially when the types are incorrect or use features not supported by the TypeScript compiler. The main goal of this is to make it easier to type existing packages using JsDoc (incorrectly).

For example, I'm contributing declaration files to a package that is currently using JsDoc types with some mismatching parameter names and syntax errors. As a result, I get a large number of TypeScript compiler errors because of the invalid syntax and conflicts with my more up to date TypeScript typings. This error and consistency checking is good default behavior, but in this case I want to override or disable the parsed JsDoc types so I can contribute typings without making unrelated changes to the package, like porting files to TypeScript (in which case I could just use TypeDoc) or removing JsDoc comments (which are needed for documentation and can be fixed later). If I disabled checkJs, I would lose TypeScript's type checking using my declaration files against the original JS sources, and it would be difficult to verify them. This would be especially useful for DefinitelyTyped, since you can't adjust the syntax of the original source code's JsDoc comments in a separate package.

An overview of the suggested solution

A compiler option named noJsdoc or disableJsdoc, defaulting to false. When enabled, JsDoc comments are not parsed and do not create type errors or affect the types of JavaScript code.

Examples of how the suggestion would work in various places

If this could live in the tsconfig, it would be easy for external tools like editor plugins to determine if they should inspect JsDoc comments for TypeScript integration, though this could be handled by the compiler or language service in most cases.

Code example

index.js

Original package source with inaccurate JsDoc types. I'm looking for a way to ignore them without modifying the original file, as it lives in someone else's package that I'm writing a typing package for.

/**
 * @type {string}
 */
export const defaultOptions = { name: "Foo" };

index.d.ts

TypeScript declaration file for index.js. Note that the type is more accurate than the JsDoc type and needs to replace it, but TypeScript compiler will fail on the JsDoc types being incompatible with the declaration file's types. I am suggesting a way for TypeScript to ignore the JsDoc types when using this TypeScript declaration file.

export const defaultOptions: { name: string };

tsconfig.json

TypeScript config for the declaration package file. Note that the new noJsdoc option is enabled, and TypeScript is only being used as a typechecker (no files are generated).

{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": true,
    "noEmit": true,
    "noJsdoc": true
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions