Kristian Roebuck
Kristian Roebuck

Reputation: 3379

Expose TypeScript's type definitions to JSDoc

I have the following code, of which, TypeScript is happy with. It correctly sees that RequestInit as the options to a fetch request. I also have the ESLint JSDoc plugin active with no-undefined-types enabled. When I run ESLint against this code I get the following error The type 'RequestInit' is undefined.

Is there a way I can expose TypeScript's built in type definitions to make JSDoc aware of them? I have the JSDoc plugin configured for TypeScript. settings.jsdoc.mode = "typescript"

// @ts-check

/** @type {RequestInit} */
const FETCH_OPTIONS = {
  cache: 'no-cache',
  credentials: 'include',
  method: 'GET',
  mode: 'cors'
};

fetch('/api/getsomething/3', FETCH_OPTIONS)
 .then(nextFn)
 .catch(errorFn)

Upvotes: 1

Views: 1238

Answers (1)

DustInComp
DustInComp

Reputation: 2666

As far as I can tell from the eslint plugin's readme, to define an external type for use in a JSDoc comment, you must either import and assign it to a variable (which isn't possible in this case), or declare it as "defined" in the rule's options like so:

// .eslintrc
{
  "rules": {
    "jsdoc/no-undefined-types": [1, {
      "definedTypes": [
        "RequestInit"
      ]
    }]
  }
}

Upvotes: 4

Related Questions