Search code examples
typescriptdefinitelytyped

Use types declaration on another package


I have setup a private NPM registry hosting a fork of a publicly available package (which is allowed by the package's licence).

The source package has a type declaration provided by the wonderful DefinitelyTyped project.

Currently, in my project, if I write:

import mypackage from '@private/mypackage'

The types provided by DefinitelyTyped (@types/mypackage) will not apply, and I will get the error:

error TS7016: Could not find a declaration file for module '@private/mypackage'. 'node_modules/@private/mypackage/index.js' implicitly has an 'any' type.

In my project, is it possible to redeclare the source package types for the fork, so that the above example will work?


Solution

  • You can add a declaration file (.d.ts) for the module:

    declare module '@private/mypackage'
    {
        import m from 'mypackage';
    
        export default m;
    }