簡體   English   中英

修復了針對 Firebase 的 Cloud Functions 類型“any”的 Typescript 警告

[英]Fix for Typescript warnings for type ‘any’ with Cloud Functions for Firebase

我收到了許多與在我的 Typscript 代碼中使用“any”作為函數返回類型有關的警告。 我正在嘗試使用 Firebase 的 Cloud Functions 編寫 node.js 后端,以管理 Google Play 結算購買和訂閱。 我在這里遵循 Classy Taxi Server 示例中給出的示例: https://github.com/android/play-billing-samples/tree/main/ClassyTaxiServer

例如,以下內容:

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;
}

發出警告

出乎意料。 指定不同的類型。 @typescript-eslint/no-explicit-any)

我試圖將“任何”更改為“未知”,但隨后出現錯誤

類型“未知”.ts(2339) 上不存在屬性“formOfPayment”

類型“未知”.ts(2339) 上不存在屬性“skuType”

在另一個 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];
        }
    });
}

我收到以下警告

function 上缺少返回類型。 @typescript-eslint/explicit-module-boundary-types

參數“firestoreObject”應使用非任何類型鍵入。 @typescript-eslint/explicit-module-boundary-types

出乎意料。 指定不同的類型。 @typescript-eslint/no-explicit-any

在這個 function 中,如果我將“任何”更改為“未知”,我仍然會收到警告

function 上缺少返回類型。

在另一個示例中,在此構造函數中使用“any”時出現錯誤:

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

再次警告是

參數“playDeveloperApiClient”應使用非任何類型鍵入。 @typescript-eslint/explicit-module-boundary-types

在這種情況下,如果我遵循使用“未知”而不是“任何”的建議,那么在以下 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);
              }
          })
      }); 

在構造函數中將 'any' 更改為 'unknown' 所產生的錯誤是:

類型“未知”.ts(2339) 上不存在屬性“購買”

如果我理解正確,我可以通過禁用整個文件的顯式模塊邊界類型和/或 no-explicit-any 來防止所有這些(和其他類似)警告而不會產生錯誤,但我不確定是否這是不好的做法嗎?

是否有另一種(更好的)方法來指定返回類型以避免使用“任何”? 還是只提前 go 並禁用顯式模塊邊界類型或無顯式任何?

如果您不想使用any ,則必須為您的類型聲明一個接口並將其設置為 function 的返回類型。

您可以執行以下操作:

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

然后,您可以將其用作任何其他 function 的返回類型,該類型要求您返回具有上述屬性的 object。 例如

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM