繁体   English   中英

如何从带有 jsdoc 但没有 typescript 的现有 npm 包中获取 vscode 中的智能感知(index.d.ts)

[英]How to get intellisense in vscode from an existing npm package with jsdoc but without typescript (index.d.ts)

使用不支持 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 注释生成 [.d.ts.][https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html]。 但这似乎是为包的所有者而不是为消费者创建的。

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

我发现的唯一方法是从 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 信息。

生成的 d.ts 文件可以移动到 node_modules/@type/adobe__pdfservices-node-sdk 文件夹。

之后,我还在此文件夹的根目录中添加了 index.d.ts

import * as lib from './pdfservices-sdk'

export = lib;

现在 Vscode 会在使用未知类型时自动建议正确的导入

import { Credentials, ExecutionContext, ExportPDF, FileRef } from "@adobe/pdfservices-node-sdk";

然后新的adobe__pdfservices-node-sdk文件夹几乎准备好在 DefinitiveTyped repro中进行 PR

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM