Reputation: 3867
Consider the following example:
interface pluginOptions {
neededProperty: ...neededPropertyType...;
... a bunch of other optional properties ...
}
interface myExtendedPluginOptions extends pluginOptions {
neededProperty?: ...neededPropertyType...;
}
What I want to achieve is, that I am using a plugin, which on initialization excepts a property (neededProperty
) to be set in the options objects that it receives. I would like to write a wrapper class around that plugin, which will feed this option object to the original plugin, but instead of relying that the object be present in the options when I instantiate my wrapper, it will fetch this value in a different way. Basically like this:
/** old init */
const plugin = plugin(pluginOptions);
/** new init */
class ExtendedPlugin {
constructor(neededProperty: ...neededPropertyType..., options: myExtendedPluginOptions) {
this.plugin = plugin(jQuery.extend(myExtendedPluginOptions, {
neededProperty: neededProperty
});
}
}
const plugin = new ExtendedPlugin(neededProperty, myExtendedPluginOptions);
The reason I need this, is that in our framework, we are using different kinds of plugins, and would like to make the usage of all of these plugin uniforms in a way for that to be easier for other developers, to work with them.
Is it possible somehow in typescript definitions, to either:
I know, that I could just copy over into a new interface type all of the properties of the old interface, but I don't consider that as an optimal solution, as it needs constant maintenance in case we update the original plugin, and a new property is added or deleted.
In the above example, typescript is complaining about making the extended interfaces neededProperty optional, with the:
Property 'neededProperty' is optional in type 'myExtendedPluginOptions' but required in 'pluginOptions' ts(2430)
Upvotes: 1
Views: 2401
Reputation: 255155
It comes as a built-in utility type Omit<T, K>
.
type myExtendedPluginOptions = Omit<pluginOptions, 'neededProperty'>;
would produce a new type that has the same properties as pluginOptions
but with neededProperty
excluded entirely.
Upvotes: 2