My goal is to create an npm package for my type definitions to be used across various projects. I plan to name it @dfoverdx/ts-magic
. Some projects are using TypeScript versions 3.x, while others are using >=4.2, which introduced new features like leading and middle rest elements in tuple types.
Is it feasible to have multiple declaration files that cater to the user's TypeScript version?
I have considered a few potential solutions that might work, but I lack the knowledge to implement them or determine if they are viable.
One approach could involve users specifying the ts-magic declaration file to include using the
lib
value in theirtsconfig.json
. Each declaration file could then reference the previous version's file using a triple-slash directive.Alternatively, I could create multiple sub-packages with dependencies on the previous versions. However, the lack of nested scope in NPM makes this option less desirable. It also raises concerns about properly typing global object methods like Object.fromEntries() and declarations within
declare global {}
, which cannot be restricted to specific files when importing the package.There might be a solution involving modifications to the
package.json
file usingpeerDependencies
or another property I am not familiar with.An option could be using a triple-slash directive, although it may not be the most effective method.
I am certain there must be a way to achieve this. Any thoughts on the best approach?