install @types definition from fork
Asked Answered
N

3

9

Context

I forked DefinitelyTyped and made a workaround to one type definition. This workaround is on a branch in this fork.

This change shouldn't be merged back, since it's only a temporary workaround.

Question

How do I install this customized type definition via npm or yarn?

Problems

  • I can't just install the branch, since it would install the whole DefinitelyTyped repo instead of just the library.
  • Ideally I'd like to have it installed the same way default type definitions are, so that no additional (global) configuration is required.
Neiman answered 4/7, 2018 at 7:20 Comment(0)
H
4

If this is just temporary like you say (or maybe a very specific use case), it probably makes most sense to just copy the modified type definitions into the repository of your project. You can then reference these definitions just like you would with any custom typings (see my other answer for instructions).

Hurleigh answered 5/11, 2018 at 4:35 Comment(1)
You would need to perform an additional step of adding the custom type definitions to "paths" in compiler options. Say you put your custom index.d.ts in src>custom_typings>[package_name] folder. You would need to add: "typeRoots": ["./node_modules/@types", "./src/custom-typings"], and "paths":{"[package_name]": ["./src/custom-typings/[package_name]"], "[package_name]/*": ["./src/custom-typings/[package_name]/*"]}Destruction
P
2

AFAIK, there's no one-step solution because npm and yarn don't support installing directly from a subdirectory of a git repository. See the issues for npm (maybe there is a newer one) and yarn and previous Stack Overflow questions including this one. (If your repository is hosted on a service that offers a URL to download a tarball of a subdirectory of the repository, you could try running npm install on that URL; I didn't pursue that approach.)

I suggest you use Braid (disclosure: I am a Braid contributor) to copy the subdirectory of the DefinitelyTyped fork into a subdirectory of your project and then run npm install (or yarn add) on that subdirectory. npm or yarn will create a symlink from the appropriate location under node_modules to the subdirectory, so TypeScript will find the type declarations without additional configuration. Since the DefinitelyTyped repository does not contain fully functional package.json files for individual types packages, you'll likely need to create the package.json file in the local subdirectory or add the missing fields to it before running npm install. (You can just leave this change as a downstream change to the Braid mirror.)

The only drawback I see in this solution is that it makes a copy of the types package in the downstream repository, which will permanently take up some space in the repository. (And Braid will copy the entire history of DefinitelyTyped into your local clone of the downstream repository, but that space can be reclaimed by garbage collection.)

Parthena answered 1/11, 2018 at 13:18 Comment(0)
F
2

This could be a time consuming process but doable (to the best of my knowledge).

Use the types-publisher project. The Manually Running section in the Readme covers the steps that needs to be executed.

The following steps specified in the Readme should be sufficient to generate and publish the package to your npm account:

  1. Update the local DefinitelyTyped repo
  2. Parse the definitions
  3. Calculate versions
  4. Generate packages on disk
  5. Publish packages on disk
  6. Publish registry

In the settings.json have the scopeName equal to npm username.

The package can be used as npm i @username/package-name

Felicio answered 1/11, 2018 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.