TypeScript声明:indexOf方法签名与Array.indexOf冲突

时间:2015-12-04 11:43:34

标签: typescript

/** Represents a read-only list of timed metadata tracks. */
declare class MediaPlaybackTimedMetadataTrackList extends Array {
    /** Returns an iterator that iterates over the items in the collection. */
    first(): Windows.Foundation.Collections.IIterator;
    /** Returns the timed metadata track at the specified index. */
    getAt(index: number): Windows.Media.Core.TimedMetadataTrack;
    /** Retrieves the timed metadata tracks that start at the specified index in the list. */
    getMany(startIndex: number): { /** The timed metadata tracks that start at startIndex in the list. */ items: Windows.Media.Core.TimedMetadataTrack; /** Retrieves the timed metadata tracks that start at the specified index in the list. */ returnValue: number; };
    /** Gets the presentation mode of the timed metadata track with the specified index. */
    getPresentationMode(index: number): Windows.Media.Playback.TimedMetadataTrackPresentationMode;
    /** Retrieves the index of a specified timed metadata track in the list. */
    indexOf(value: Windows.Media.Core.TimedMetadataTrack): { /** If the timed metadata track is found, this is the zero-based index of the audio track; otherwise, this parameter is 0. */ index: number; /** True if the timed metadata track is found; otherwise, false. */ returnValue: boolean; };
    /** Occurs when the presentation mode of the MediaPlaybackTimedMetadataTrackList changes. */
    onpresentationmodechanged: (ev: Windows.Foundation.TypedEventHandler) => any;
    addEventListener(type: "presentationmodechanged", listener: (ev: Windows.Foundation.TypedEventHandler) => any): void;
    removeEventListener(type: "presentationmodechanged", listener: (ev: Windows.Foundation.TypedEventHandler) => any): void;
    /** Sets the presentation mode of the timed metadata track with the specified index. */
    setPresentationMode(index: number, value: Windows.Media.Playback.TimedMetadataTrackPresentationMode): void;
    /** Gets the number of timed metadata tracks in the list. */
    size: number;
    addEventListener(type: string, listener: EventListenerOrEventListenerObject): void;
    removeEventListener(type: string, listener: EventListenerOrEventListenerObject): void;
}

这是script生成的当前类型定义。这个类在其原型链中有Array,它有自己的indexOf方法,其签名与Array.indexOf冲突。这种冲突使编译器继续抱怨,我需要让它保持沉默。是否有一种已知的方法可以使这项工作?

这里的解决方案必须表明该类继承自Array。

注意:我无法修改语义,因为它是one of the current UWP APIs

2 个答案:

答案 0 :(得分:2)

您需要在原始实现中包含相同的签名,因此请从

开始
declare class MediaPlaybackTimedMetadataTrackList extends Array {
    indexOf(value: any): { index: number; returnValue: boolean; };
    indexOf(searchElement: any, fromIndex?: number): number;
}

但显然这是有问题的,因为如果实施不尊重fromIndex&#39},那么number中提供的任何内容都不会获得Array。原来的行为。

所以你可以做的是:

declare class MediaPlaybackTimedMetadataTrackList extends Array {
    indexOf(value: any): { index: number; returnValue: boolean; };
    indexOf(value: any, ...extra: any[]): { index: number; returnValue: boolean; };
    indexOf(searchElement: any, fromIndex?: number): number;
}

这有点像黑客,但你现在不能再获得原始实现,因为调用者总是会解决第二次重载。

如果您想尝试避免调用第二个重载,也可以让它返回void

declare class MediaPlaybackTimedMetadataTrackList extends Array {
    indexOf(value: any): { index: number; returnValue: boolean; };
    indexOf(value: any, ...extra: any[]): void;
    indexOf(searchElement: any, fromIndex?: number): number;
}

{ "This type should never be used, stop what you're doing": void }

答案 1 :(得分:1)

可持续OOP的关键是遵循Liskov substitution principle。因此派生的数组应该仍然是 Array 。因此,我们应该使用重载原始 indexOf 定义:

declare class MediaPlaybackTimedMetadataTrackList extends Array {
    ...
    indexOf(searchElement: Windows.Media.Core.TimedMetadataTrack, fromIndex?: number)
       : number;
    indexOf(value: Windows.Media.Core.TimedMetadataTrack)
       : { index: number; returnValue: boolean; };
}