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

Viewed 64

Using an npm package without typescript support give an error

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)

creating an .d.ts file is not really an option. The type is still missing or you have add the required types by hand.

There is also possible to generate [.d.ts.][https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html] from the jsdoc comments with typescript compiler. But this seem to created for the owner of the package and not for the consumer.

Question: What is the best way to add intellisense of an npm package with jsdoc comments

1 Answers

The only way I found was to open the npm package in vscode from the node_modules. Add the tsconfig.json

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

and run tsc. and correct the wrong jsdoc types and add some @type information needed by typescript.

The generated d.ts files can be moved to the node_modules/@type/adobe__pdfservices-node-sdk folder.

After that I also added the index.d.ts in the root of this folder

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

export = lib;

Now Vscode will automatic suggest the correct import when using an unknown type

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

Then the new adobe__pdfservices-node-sdk folder is almost ready for a PR in the DefinitelyTyped repro

Related