Я пытаюсь написать декларации для вспомогательного модуля Google Maps для node, но у меня возникают проблемы с PromiseConstructorLike, которые ожидают библиотеки, и правильно верните им методы экземпляра "PromiseLike" (в соответствии с https://googlemaps.github.io/google-maps-services-js/docs/[email protected]_maps.html):
Promise function <optional> Promise constructor (optional).
так что я сделал (разделился на интересные биты):
declare namespace GoogleMaps {
export interface CreateClientOptions<T> {
/** Promise constructor (optional). */
Promise?: T;
}
export interface GoogleMapsClient<T> {
directions<U>(query, callback?: ResponseCallback<U>): RequestHandle<U, T>;
}
export interface Response<U extends any> {
headers: any;
json: U;
status: number;
}
export interface RequestHandle<U, T extends PromiseLike<Response<U>>> {
asPromise(): T;
cancel(): void;
finally(callback: ResponseCallback<U>): void;
}
export type ResponseCallback<U> = (err: Error, result: Response<U>) => void;
export function createClient<T extends PromiseConstructorLike>(options: CreateClientOptions<T>): GoogleMapsClient<T>;
}
declare module '@google/maps' {
export = GoogleMaps
}
Конечно, это не сработает, если я использую, например, Bluebird в createClient
как
import * as bluebird from 'bluebird'
import { createClient } from '@google/maps'
createClient({ Promise: bluebird }).directions({}).asPromise()/** no "then" here, just the static methods from Bluebird, like Bluebird.all */
Вопрос заключается в следующем:
В любом случае, я могу намекать на метод asPromise
, чтобы возвращать методы экземпляра (затем, catch, finally, сокращение, тайм-аут и т.д.) из bluebird без необходимости расширения интерфейса RequestHandle
вручную?
Дополнительная информация (lib.d.ts
объявления):
PromiseConstructorLike
:
declare type PromiseConstructorLike = new <T>(executor: (resolve: (value?: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void) => PromiseLike<T>;
PromiseLike
:
interface PromiseLike<T> {
/**
* Attaches callbacks for the resolution and/or rejection of the Promise.
* @param onfulfilled The callback to execute when the Promise is resolved.
* @param onrejected The callback to execute when the Promise is rejected.
* @returns A Promise for the completion of which ever callback is executed.
*/
then(
onfulfilled?: ((value: T) => T | PromiseLike<T>) | undefined | null,
onrejected?: ((reason: any) => T | PromiseLike<T>) | undefined | null): PromiseLike<T>;
}