How to use DefinitelyTyped types on forked packages?
Asked Answered
W

2

7

For packages that don't come with it's own types, next step is to look for @types/<package> for the package typings. However, if there is a package X, which is forked to a package Y. How can we use the same @types/<X> package for the package Y?

Edit:

Example: node-bunyan doesn't have types. @types/bunyan exists and provides types. Now, I am trying to use a fork of node-bunyan(@scoop/bunyan) while still being able to utilize types from @types/bunyan.

Within answered 11/11, 2020 at 10:56 Comment(0)
A
6

Update:

By looking at this and this.

Probably you can add in your tsconfig.json

   "paths":{
      "@scoop/bunyan": ["./node_modules/@types/bunyan"]
    }

Maybe you can add you to the package Y in package.json the path to @types/<X>.

{
    ...
    "types": "./node_modules/@types/<X>/lib/main.d.ts",
    ...
}
Anthropometry answered 11/11, 2020 at 11:7 Comment(4)
This needs me to change the fork's package.json. Also, @types is mostly pluggable. Adding types to repos that don't have types without changing their package.json which is what I am looking for as well. Example: node-bunyan doesn't have types. @types/bunyan exists and provides types. Now, I am trying to use a fork of node-bunyan while still being able to utilize types from @types/bunyan. Of course, it simply might not be possible to do it at all. Your solution probably would be a workaround in that case. Thanks!Within
@Querious I updated the answer with a possible solution!Anthropometry
@Anthropometry In the second solution, do I also have to add @types/<X> to the package's package.json? I can't get either solution to work. :(Psychoneurotic
I got the second solution to work in the end. As I suspected, I had to add the @types/<X> package. I then had a typo in the "types" path (triple check it!).Psychoneurotic
A
2

You could pretend to NPM that the DT types have a different package name which matches your fork.

npm i -D @types/scoop__bunyan@npm:@types/node-bunyan

(This did the job for me just now -- I was suggested this idea by Gerrit0 on the Typescript Discord.)

Alpenhorn answered 22/12, 2022 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.