Search code examples
typescript

Map types from another module


I've forked a large module foo which also has a type declarations library @types/foo. I've renamed the new fork to @org/foo. The type declarations won't change for the fork. Is there a way to just map @types/foo to the new name @org/foo? For example: @org/types-foo without forking the whole @types module as well?


Solution

  • You can install the fork using the original package name as an alias — for example:

    npm install foo@npm:@org/foo
    

    This will include the benefit of not needing to refactor any import specifiers in your codebase — only to replace the dependency.


    More information is available at the documentation for npm install:

    npm install <alias>@npm:<name>
    

    Install a package under a custom alias. Allows multiple versions of a same-name package side-by-side, more convenient import names for packages with otherwise long ones, and using git forks replacements or forked npm packages as replacements. Aliasing works only on your project and does not rename packages in transitive dependencies. Aliases should follow the naming conventions stated in validate-npm-package-name.

    Examples:

    npm install my-react@npm:react
    npm install jquery2@npm:jquery@2
    npm install jquery3@npm:jquery@3
    npm install npa@npm:npm-package-arg