I have written and npm published this: https://github.com/justin-calleja/pkg-dependents
Now I'm writing this package in Typescript: https://github.com/justin-calleja/update-dependents
I want to re-use a type defined in pkg-dependents (IndexInfoDict) in update-dependents and I want to check whether there's a better way of doing it than this:
In pkg-dependents's index.ts:
import { IndexInfoDict } from './interfaces';
export interface IndexInfoDict extends IndexInfoDict {};
i.e. I'm importing the IndexInfoDict interface to annotate a function in pkg-dependents's index.ts, but because I want to use this same type in update-dependents, I am forced to export another IndexInfoDict which extends itself…
It seems like a weird pattern but the compiler is now happy.
Note: additional steps taken to share the type:
import { IndexInfoDict } from 'pkg-dependents/lib/index.d.ts';Can someone confirm that there isn't a better way?
What I would like, but don't know if it's possible, is to:
import { pkgDependents, IndexInfoDict } from 'pkg-dependents' ... but since 'pkg-dependents' refers to a JS file (pkg-dependents's package.json's main is ./lib/index.js), I doubt whether this is possible.Edit 1:
The more I think about it the more 2 above (avoid 'pkg-dependents/lib/index.d.ts') seems impossible. I was hoping tsc has some magic in it to figure out to get type definitions from 'index.d.ts' even though 'index.js' is being imported - that way I can just import { pkgDependents, IndexInfoDict } from 'pkg-dependents' from a Typescript project and just import { pkgDependents } from 'pkg-dependents' from a JS project (no tsc magic and JS doesn't have types anyway so IndexInfoDict is out).
Currently, I have to:
import pkgDependents from 'pkg-dependents';
import { IndexInfoDict } from 'pkg-dependents/lib';
So ok... maybe there's no magic. What about 1? Is there a less confusing way to export an imported type other than export one which extends itself?
(I'm using IndexInfoDic in more than one place in pkg-dependents. One of which happens to be index.ts, the main exported function. Because of this, I want it outside index.ts but exported from index.ts so it's generated in index.d.ts and users get the types used by the main function from index.d.ts).
In your index.ts, You can do:
export { IndexInfoDict } from "./interfaces;
This will export the interface from the main file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With