How to implement a method for a certain types of arrays with typescript

Viewed 20

I want to implement a method to filter some types of array in a certain way. So far I could achive this with this code:

  const isActiveBarSettingsBarF = (
        settingsBar: ISettingsBar[],
        bar: number
    ) => settingsBar[bar - 1].active !== false;

declare global {
    interface Array<T> {
        isActiveBarSettingsBar(settingsBar: ISettingsBar[]): ISettingsBar[];
    }
}

Array.prototype.isActiveBarSettingsBar = function isActiveBarSettingsBar(
    settingsBar: ISettingsBar[]
) {
    return this.filter((b: ISettingsBar) =>
        isActiveBarSettingsBarF(settingsBar, b.bar)
    );

The problem is this method is accessible for any type of array and I would like only to be for arrays of type ISettingsBar[].

If instead I do something like:

declare global {
    interface Array<ISettingsBar> {
        isActiveBarSettingsBar(settingsBar: ISettingsBar[]): ISettingsBar[];
    }
}

I get an error saying, all declarations of array must have identical type parameters.

Any idea? Thank you very much

0 Answers
Related