簡體   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