In my typescript project, I am utilizing multiple cordova plugins. Each plugin is initialized on a global plugins
object as shown below:
// Plugin A init
window.plugins.pluginA = {...};
// Plugin B init
window.plugins.pluginB = {...};
However, I am facing an issue regarding how to structure the types so that each plugin can extend the plugins
object without being aware of other plugins. The problem arises when defining the type for the second plugin, as it throws an error due to the plugins
object already being defined. How should I organize it to allow for extension instead?
// Type declarations for Plugin A
interface IPluginA {
methodA: (foo: number) => void
}
interface Window {
plugins: {
pluginA: IPluginA;
};
}
// Type declarations for Plugin B
interface IPluginB {
methodB: (bar: string) => void
}
interface Window {
// ERROR: Subsequent property declarations must have the same type.
// Property 'plugins' must be of type '{pluginA: IPluginA};'
plugins: {
pluginB: IPluginB;
};
}