Typescript存在具有保留字的命名空间

时间:2018-02-26 15:58:06

标签: node.js typescript paypal typescript-typings paypal-rest-sdk

我想升级"paypal-rest-sdk" typings。此SDK包含方法Bitmap bitmap = BitmapFactory.decodeResource(mainImageUri);;

payment.authorization.void(...)上不存在

void方法,我试图通过本地声明覆盖它。

代码示例:

@types/paypal-rest-sdk

但是这段代码不起作用,当我尝试调用declare module "paypal-rest-sdk" { export namespace authorization { function void(): any; } } export const paypal = Paypal; 方法时,typescript会向我显示错误:void

1 个答案:

答案 0 :(得分:1)

The only way I can see to provide a type definition that includes a method named void (sigh) is using a variable rather than a namespace.

Here is an example based on the current definition on Definitely Typed (but with the authorization namespace converted to a variable).

export var authorization: {
    get: (
        id: string,
        config: http.RequestOptions | CallbackFunction<AuthorizationResource>,
        cb?: CallbackFunction<AuthorizationResource>) => void;
    capture: (
        id: string,
        data: CaptureRequest | http.RequestOptions | CallbackFunction<CaptureResource>,
        config?: http.RequestOptions | CallbackFunction<CaptureResource>,
        cb?: CallbackFunction<CaptureResource>) => void;
    reauthorize: (
        id: string, data: Amount | http.RequestOptions | CallbackFunction<AuthorizationResource>,
        config?: http.RequestOptions | CallbackFunction<AuthorizationResource>,
        cb?: CallbackFunction<AuthorizationResource>) => void;
    void: (data?: any, config?: http.RequestOptions, cb?: CallbackFunction<any>) => void;
}

Specific changes:

  • export namespace authorization changed to export var authorization
  • namespace body converted to type annotation
  • each export function converted to member
  • each function converted to type annotation

This can be accessed without type errors:

...authorization.void(data, config, callback);