Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
315 views
in Technique[技术] by (71.8m points)

node.js - Fix for Typescript warnings for type ‘any’ with Cloud Functions for Firebase

I’m getting a number of warnings all relating to the use of ‘any’ as a return type for functions in my Typscript code. I am trying to write a node.js backend with Cloud Functions for Firebase, to manage Google Play Billing purchases and subscriptions. I am following the examples given in the Classy Taxi Server example here: https://github.com/android/play-billing-samples/tree/main/ClassyTaxiServer

For example, the following:

function purchaseToFirestoreObject(purchase: Purchase, skuType: SkuType): any {
    const fObj: any = {};
    Object.assign(fObj, purchase);
    fObj.formOfPayment = GOOGLE_PLAY_FORM_OF_PAYMENT;
    fObj.skuType = skuType;
    return fObj;
}

Gives the warning

Unexpected any. Specify a different type. @typescript-eslint/no-explicit-any)

I have tried to change ‘any’ to ‘unknown’, but then I get an error

Property 'formOfPayment' does not exist on type 'unknown'.ts(2339)

and

Property 'skuType' does not exist on type 'unknown'.ts(2339)

In another function

export function mergePurchaseWithFirestorePurchaseRecord(purchase: Purchase, firestoreObject: any) {
    // Copy all keys that exist in Firestore but not in Purchase object, to the Purchase object (ex. userID)
    Object.keys(firestoreObject).map(key => {
        // Skip the internal key-value pairs assigned by convertToFirestorePurchaseRecord()
        if ((purchase[key] === undefined) && (FIRESTORE_OBJECT_INTERNAL_KEYS.indexOf(key) === -1)) {
            purchase[key] = firestoreObject[key];
        }
    });
}

I get the following warnings

Missing return type on function. @typescript-eslint/explicit-module-boundary-types

Argument 'firestoreObject' should be typed with a non-any type. @typescript-eslint/explicit-module-boundary-types

Unexpected any. Specify a different type. @typescript-eslint/no-explicit-any

In this function, if I change ‘any’ to ‘unknown’, I still get a warning for

Missing return type on function.

In another example I get an error for the use of ‘any’ in this constructor:

export default class PurchaseManager {
   constructor(private purchasesDbRef: CollectionReference, private playDeveloperApiClient: any) { }

And again the warning is

Argument 'playDeveloperApiClient' should be typed with a non-any type. @typescript-eslint/explicit-module-boundary-types

In this case, if I follow the suggestion to use ‘unknown’ instead of ‘any’, then I get an error for ‘purchases’ in the following function:

      const apiResponse = await new Promise((resolve, reject) => {
          this.playDeveloperApiClient.purchases.products.get({
              packageName: packageName,
              productId: sku,
              token: purchaseToken,
          }, (err, result) => {
              if (err) {
                  reject(this.convertPlayAPIErrorToLibraryError(err));
              } else {
                  resolve(result.data);
              }
          })
      }); 

The error created by changing ‘any’ to ‘unknown’ in the constructor is:

Property 'purchases' does not exist on type 'unknown'.ts(2339)

If I understand correctly, I could prevent all of these (and other similar) warnings without creating errors, by disabling explicit-module-boundary-types, and/or no-explicit-any for the entire file, but I am not sure if this is bad practice?

Is there another (better) way to specify the return types to avoid using ‘any’? Or is it fine to just go ahead and disable explicit-module-boundary-types or no-explicit-any?

question from:https://stackoverflow.com/questions/65833446/fix-for-typescript-warnings-for-type-any-with-cloud-functions-for-firebase

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you do not want to use any, you will have to declare an interface for your type and set that as the return type of the function.

You can do something like:

interface MyType {
    formOfPayment: string;
    skyType: SkyType;
    foo: //<any other pre-existing type>;
}

Then you can use this as the return type of any other function that requires you to return an object with above properties. e.g.

function purchaseToFirestoreObject(purchase: Purchase, skuType: SkuType): MyType {
    const fObj: any = {};
    Object.assign(fObj, purchase);
    fObj.formOfPayment = GOOGLE_PLAY_FORM_OF_PAYMENT;
    fObj.skuType = skuType;
    return fObj;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...