1

How do I add types for a submodule of a node module? For example, say I install a module called foo, which has a submodule bar, which is imported like this:

import Bar from "foo/bar";

I tried the following, but it didn't appear to work (in types/styled-components/primitives.d.ts):

declare module "foo/bar" {
   // types
}

If it helps, the specific module I am trying to work with is styled-components/primitives, so if there are already types available somewhere I'm happy to pull those instead of defining them myself. @types/styled-components did not appear to include them.

treyhakanson
  • 4,611
  • 2
  • 16
  • 33
  • Can you show some concrete code with `styled-components/primitives` you tried to get working? – ford04 Aug 18 '19 at 08:48
  • @ford04 here is a gist of some code I included within `node_modules/@types/styled-components/primitives.d.ts` in the meantime. It basically just mirrored `styled-components/native`, so its not really correct but it was a temporary solution: https://gist.github.com/treyhakanson/2fd8c792e4811c00b995a39e4141faac – treyhakanson Aug 18 '19 at 20:31
  • Sorry for posting late. Seems like styled-components does neither offer the type in its package, nor in @types. So you will have to define your own type module. As a start: Remove any import/export statement in `primitives.d.ts` and write `declare module "styled-components/primitives";` in it to type it as any. You may also have a look at [1](https://stackoverflow.com/questions/57453347/typescript-merge-interfaces-from-multiple-files/57459260), [2](https://stackoverflow.com/questions/57441879/bypass-implicitany-with-a-module-type/57443849#57443849) (disclaimer: that are my answers). – ford04 Aug 22 '19 at 17:49
  • Let me know, if that helps out. – ford04 Aug 22 '19 at 17:50

0 Answers0