16

I'm using TypeScript and I want to use a scoped package (e.g. @foo/bar, @babel/core, etc.) that doesn't ship its own type declarations.

I've tried to run something like

npm install @types/@foo/bar

but it doesn't seem to be available.

Is there any way to get these .d.ts for these packages into the @types scope? Is there a way to write my own scoped packages on DefinitelyTyped if I need to?

1 Answer 1

23

Is there any way to get these .d.ts for these packages into the @types scope?

Yes there is a way! It's just a little unintuitive.

npm doesn't permit scoped packages to contain @ in their name, so these names are mangled to use two underscores in place of the @.

So as an example, if you want to install type declarations for the package @foo/bar, you'll need to run

npm install @types/foo__bar

Is there a way to write my own scoped packages on DefinitelyTyped if I need to?

Yes! From the Definitely Typed README.md:

Types for a scoped package @foo/bar should go in types/foo__bar. Note the double underscore.

2
  • 1
    Is there a way to write my own scoped packages outside DefinitelyTyped if I need to? -- DefinitelyTyped is just too slow to accept pull requests...
    – Polv
    Sep 15, 2019 at 17:47
  • 2
    I like this answer but as a nitpick maybe it should be noted that the two underscores are not "in place of the @" - it's not @types/__foo/bar. The two underscores replace both the @ and the /. In other words, the @ is removed and the / is replaced by __: @scope/package => scope__package.
    – JHH
    Jun 2, 2021 at 7:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.