What dependency type should @types be for a published package?
Asked Answered
G

1

5

I am creating a package that I intend to publish to NPM. I am writing the package in typescript, and have my tsconfig file set up to emit the typescript declaration file along with the compiled javascript so someone else using typescript can get proper type info in their IDE - pretty standard. I am making use of some of the DefinitelyTyped packages, but am unsure of what dependency type they should be. If someone is using my package in a typescript project, my declaration files will need the relevant @types packages installed, but if they are using javascript they won't need these installed. I'm thinking the @types packages should be in optionalDependencies of my package's package.json file. Is this correct?

Gaussmeter answered 28/2, 2019 at 13:1 Comment(1)
similar (duplicate?) question: How do I decide whether @types/* goes into dependencies or devDependencies?Backwardation
G
9

According to Brian Terlson, ranged peer dependencies should be used as a rule of thumb.

Normal dependency (not recommended)

You pick the version of the @types, but there is no guarantee it will match the version your consumers use. For example, you might pick @types/[email protected] when your users are on Node 11.

Development dependency (not recommended)

Because they are not installed when your consumers install your package, they will need to add it to their project themselves (assuming they are using TypeScript).

Development dependency mirrored in your source code (sometimes recommended)

If your usage of @types/* is minimal, it may make sense to mirror the used type definition in your code. Mimic what the third party definitions are doing and keep it in your code. The downside is having to keep your own definitions in sync with their original version.

Peer dependency (recommended)

A middle ground. They will be installed and reused by your package and your consumers' packages. The only problem that comes to mind is when multiple libraries depend on some @types/*, but their versions don't match.

Gelasius answered 28/2, 2019 at 13:28 Comment(1)
Thanks for the info! Looks like Peer Dependency may be the best choice then and gives the user the choice of installing the @types package if it's relevant to themGaussmeter

© 2022 - 2024 — McMap. All rights reserved.