【问题标题】:Expose TypeScript's type definitions to JSDoc将 TypeScript 的类型定义暴露给 JSDoc
【发布时间】:2021-05-18 11:50:36
【问题描述】:

我有以下代码,TypeScript 对此很满意。它正确地将RequestInit 视为fetch 请求的选项。我还启用了启用 no-undefined-types 的 ESLint JSDoc 插件。当我针对此代码运行 ESLint 时,我收到以下错误 The type 'RequestInit' is undefined

有没有办法可以公开 TypeScript 的内置类型定义以使 JSDoc 了解它们?我为 TypeScript 配置了 JSDoc 插件。 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)

【问题讨论】:

  • 您是否尝试过对@types/react 的显式类型引用,如下所述:typescriptlang.org/docs/handbook/…
  • @DustInCompetent 我尝试使用/// <reference lib="dom" />,因为这是RequestInit 所在的位置,而JSDoc 仍然不知道这些类型。

标签: typescript jsdoc


【解决方案1】:

据我从 eslint 插件的readme 得知,要定义用于 JSDoc 注释的外部类型,您必须导入并将其分配给变量(在这种情况下这是不可能的),或在规则的选项中将其声明为“已定义”,如下所示:

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

【讨论】:

    猜你喜欢
    • 2016-08-09
    • 2015-08-08
    • 1970-01-01
    • 2011-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-18
    • 2011-04-02
    • 1970-01-01
    相关资源
    最近更新 更多