Is there any way to override the value of a type exported and used by a TypeScript package?
So I have this in my typings file:
// index.d.ts
declare module 'my-package' {
namespace DependencyInjection {
type ServiceID = 'myServiceOne' | 'myServiceTwo'; // original type is "any";
}
}
and I get the following the error:
Duplicate identifier 'ServiceID'.ts(2300)
inject.ts(44, 10): 'ServiceID' was also declared here.
I'd like to be able to replace the type of ServiceID
from the consuming code. Can it be done? I know interfaces can be extended but I haven't found a way to change or update a type.
Context
I'm writing a TypeScript package and I'd like users to be able to override some default types with their own when they integrate the package, rather than having to provide generics every time they use some methods.
Module augmentation only work if the module can be extended. Types cannot be extended, but interfaces can.
declare module 'my-package' {
namespace DependencyInjection {
interface ServiceIDMap {}
type ServiceID = keyof ServiceIDMap
}
}
declare module 'my-package' {
namespace DependencyInjection {
// insert your keys in here.
interface ServiceIDMap {
myServiceOne: unknown
mySevriceTwo: unknown
}
}
}
Playground
@types/jest
is designed to do this too, with ts-jest
extending the Config.InitialOptions.globals
struct.
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