可以使用条件类型从数组中提取项。将阵列重载的可用性限制为仅当
T
类型是一个数组,我们可以为
this
参数只有当调用目标符合
这
参数类型。
export type SubscriptionCallback<T = any, TTarget = void> = (this: TTarget, val: T) => void;
export interface Subscription {
dispose(): void;
}
// The conditional type that extracts the array item if T is an array
type ItemIfArray<T> = T extends Array<infer U> ? U : never;
export interface SubscribableFunctions<T = any> extends Function {
init<S extends Subscribable<any>>(instance: S): void;
notifySubscribers(valueToWrite?: T, event?: string): void;
// this must be something compatible with SubscribableFunctions<any[]> which should mean a SubscribableFunctions where T is an array
subscribe<TTarget = void>(this: SubscribableFunctions<any[]>, callback: SubscriptionCallback<Array<ArrayChange<ItemIfArray<T>>>, TTarget>, callbackTarget: TTarget, event: "arrayChange"): Subscription;
subscribe<TTarget = void>(callback: SubscriptionCallback<T, TTarget>, callbackTarget?: TTarget, event?: "change"): Subscription;
// since the last overload has the event of type string it allows invocation with arrayChange so I removed it to get the error below, not 100% sure if keeping this overload is a good idea.
//subscribe<X = any, TTarget = void>(callback: SubscriptionCallback<X, TTarget>, callbackTarget: TTarget, event: string): Subscription;
getSubscriptionsCount(event?: string): number;
}
export interface Subscribable<T = any> extends SubscribableFunctions<T> { }
export interface ArrayChange<T = any> {
status: "added" | "deleted" | "retained";
value: T;
index: number;
moved?: number;
}
let a: Subscribable<number>
a.subscribe(() => { }, void 0, 'arrayChange') //err overload not available for this type parameter
let ar: Subscribable<number[]>
ar.subscribe(a => a[0].value.toExponential() , void 0, 'arrayChange') //ok