2

I am trying to use Google Pay Passes with Typescript. There is a package to define the type: npm install --save @types/gapi but I don't have any method to use

gapi.savetoandroidpay.render("dom-container",{
  "jwt": "JWT",
  "onsuccess": "successHandler",
  "onfailure": "failureHandler"
});

Here is the documentation

Am I using the wrong package or is this missing?

  • Most likely, type declarations haven't been updated to include this method. Declare it yourself and then creative a pr to definitely typed that adds it – Aluan Haddad Nov 13 '19 at 15:48

2 Answers2

0

The @types/gapi package does not contain definitions to Google Pay implementations such as gapi.savetoandroidpay (as of Nov-26-2019).

Until the types are updated, I'd suggest declaring gapi as a type in your global.d.ts, or the TypeScript file where you use gapi.savetoandroidpay.render().

sample.ts

declare var gapi: any;

const init=()=>{
  gapi.savetoandroidpay.render("dom-container",{
     "jwt": "JWT",
     "onsuccess": "successHandler",
     "onfailure": "failureHandler"
  });
}

Important to note that will only suppress type errors.

chouxy-dev
  • 47
  • 3
0

I use the following type definition :

declare namespace gapi {}
declare namespace gapi.savetoandroidpay {
  type ButtonHeight = "small" | "standard";
  type ButtonSize = "matchparent" | undefined;
  type ButtonTextSize = "large" | undefined;
  type ButtonTheme = "dark" | "light";

  type SuccessHandler = (detail: { walletObjectIds: string[] }) => void;
  type FailureHandler = (error: Error) => void;
  type ProvideJwtHandler = () => string;

  interface ButtonOptions {
    jwt: string;
    height?: ButtonHeight;
    size?: ButtonSize;
    textsize?: ButtonTextSize;
    theme?: ButtonTheme;

    onSuccess?: SuccessHandler;
    onFailure?: FailureHandler;
    onProvideJwt?: ProvideJwtHandler;
  }

  function render(domId: string | Element, options: ButtonOptions): void;
}

You can this to a gapi.savetoandroidpay.d.ts file on your project.

This type definition originally came from https://github.com/google-pay/save-to-google-pay-button/blob/main/src/lib/button-manager.ts

Cyril Durand
  • 15,834
  • 5
  • 54
  • 62