【问题标题】:How to get intellisense in vscode from an existing npm package with jsdoc but without typescript (index.d.ts)如何从带有 jsdoc 但没有 typescript 的现有 npm 包中获取 vscode 中的智能感知(index.d.ts)
【发布时间】:2022-06-18 00:28:04
【问题描述】:

使用不支持 typescript 的 npm 包会报错

Could not find a declaration file for module '...'. '...' implicitly has an 'any' type.
  Try `npm i --save-dev @types/adobe__pdfservices-node-sdk` if it exists or add a new declaration (.d.ts) file containing `declare module '@adobe/pdfservices-node-sdk';`ts(7016)

创建 .d.ts 文件并不是一个真正的选择。类型仍然缺失或您已手动添加所需类型。

还可以使用 typescript 编译器从 jsdoc cmets 生成 [.d.ts.][https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html] .但这似乎是为包的所有者而不是为消费者创建的。

问题:使用 jsdoc cmets 添加 npm 包的智能感知的最佳方法是什么

【问题讨论】:

    标签: typescript visual-studio-code


    【解决方案1】:

    我发现的唯一方法是从 node_modules 打开 vscode 中的 npm 包。 添加 tsconfig.json

    {
      // Change this to match your project
      "include": ["src/**/*"],
      "compilerOptions": {
        "allowJs": true,
        "declaration": true,
        "emitDeclarationOnly": true,
        "declarationMap": true
      }
    }
    

    然后运行 ​​tsc。并更正错误的 jsdoc 类型并添加一些打字稿所需的@type 信息。

    并将导入行更新为:

    import PDFServicesSdk from "@adobe/pdfservices-node-sdk/src/pdfservices-sdk";
    

    这样做的问题是节点文件夹中的 .d.ts 文件必须提交到 git

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      • 2018-01-18
      • 2021-07-31
      • 1970-01-01
      • 2022-07-04
      • 2022-06-17
      • 2016-12-28
      相关资源
      最近更新 更多