Skip to content

Add TypeScript definitions #215

@samholmes

Description

@samholmes

Problem

When using this package in a typescript project, the typescript compiler will error on packages imported without type information:

❯ npm run build

> test-out-pear-api@1.0.0 build
> tsc

src/index.ts:1:17 - error TS7016: Could not find a declaration file for module 'hyperdht'. '/Users/samholmes/Developer/test-out-pear-api/node_modules/hyperdht/index.js' implicitly has an 'any' type.
  Try `npm i --save-dev @types/hyperdht` if it exists or add a new declaration (.d.ts) file containing `declare module 'hyperdht';`

1 import DHT from 'hyperdht'
                  ~~~~~~~~~~


Found 1 error in src/index.ts:1

In addition to this, developer experience is diminished when editor integrations complain:

Image

Solution

Add a index.d.ts file to the project describing the exported types from the package and include this in the "types" field in package.json:

export default class DHT {
  constructor()
  // ...
}

Footnote

I'm going to fork this repo and begin work on this effort to improve the project's documentation through type definitions and JSDocs. I'll be reaching out to the appropriate persons to make sure I get the types mostly correct before submitting a pull-request.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions