Search code examples
angulartypescriptgoogle-signindefinitelytypedfedcm

Error: Object literal may only specify known properties, and 'use_fedcm' does not exist


Problem

We're migrating our app from Google Sign-in JavaScript library (AKA gapi) to FedCM. In the email Google sent last week about migrating to FedCM, they link to steps to test your existing application with FedCM. The setup for this is to add use_fedcm: true to your gapi call. From Google's instructions:

Set use_fedcm to true when initializing the Google Sign-in platform library in your web app. Typically, initialization looks like:

gapi.client.init({use_fedcm: true}), or

gapi.auth2.init({use_fedcm: true}), or

gapi.auth2.authorize({use_fedcm: true}).

After following their recommendation, our gapi call looks like this:

await gapi.client.init({
    apiKey: API_KEY,
    clientId: CLIENT_ID,
    scope: SCOPES,
    use_fedcm: true, // newly-added
});

Now when building, I get the error:

TS2345: Argument of type '{ apiKey: string; clientId: string; scope: string; use_fedcm: boolean; }' is not assignable to parameter of type '{ apiKey?: string; discoveryDocs?: string[]; clientId?: string; scope?: string; hosted_domain?: string; }'. Object literal may only specify known properties, and 'use_fedcm' does not exist in type '{ apiKey?: string; discoveryDocs?: string[]; clientId?: string; scope?: string; hosted_domain?: string; }'.

use_fedcm: true,

Opening localhost shows the error Cannot GET / presumably because the build failed.

Attempted Solutions

1

Trying to suppress with // @ts-ignore resulted in the same build error.

// @ts-ignore
await gapi.client.init({
    apiKey: API_KEY,
    clientId: CLIENT_ID,
    scope: SCOPES,
    use_fedcm: true,
});

This made me think it's an issue with the type definitions.

2

Since it looks like a type issue, I tried updating the gapi type definitions with:

npm install --save-dev @types/gapi

I still get the same build error.

Taking a look at the latest types file, use_fedcm isn't one of the arguments listed for gapi.client.init().

It seems unlikely that Google would recommend a line of code (gapi.client.init({use_fedcm: true})) that results in a build error. What am I missing?


Solution

  • You should raise a issue on DefinetlyTyped for them to add this property until then just cast it to any since it is mentioned in the official docs:

    await gapi.client.init({
        apiKey: API_KEY,
        clientId: CLIENT_ID,
        scope: SCOPES,
        use_fedcm: true,
    } as any);