使用TypeScript和参数化变量为Gen 2 Firebase函数设置region属性时的类型不匹配问题

xxls0lw8  于 2023-08-07  发布在  TypeScript
关注(0)|答案(2)|浏览(96)

问题描述:

由于类型不匹配问题,我在使用TypeScript和参数化变量设置Gen 2 Firebase函数的region属性时遇到了麻烦。下面是我的.env文件的示例:

LOCATION="australia-southeast1"

字符串
我尝试在全局和函数级别定义region

1.使用setGlobalOptions全局定义地域

我更喜欢使用setGlobalOptions全局定义region
setGlobalOptions上的全局region属性的类型为GlobalOptions.region?: string | Expression<string> | ResetValue | undefined。这与函数级别类型HttpsOptions.region?: string | string[] | Expression<string> | ResetValue | undefined略有不同,因为全局级别只允许单个string,而不允许string[]
无论我在全球层面做什么,都是失败的。所有这三次尝试都会给予部署错误Error: Illegal type coercion of param LOCATION to type string[]

setGlobalOptions({
  region: defineString("LOCATION"),
});

x

setGlobalOptions({
  region: defineString("LOCATION") as unknown as string,
});
setGlobalOptions({
  region: defineList("LOCATION") as unknown as string,
});

的字符串
因此,我不确定如何使用参数化变量在setGlobalOptions上定义region

2.功能层定义地域

当我尝试在函数级别定义region时,IDE将region显示为HttpsOptions.region?: string | string[] | Expression<string> | ResetValue | undefined
我认为defineString()会工作,因为它返回一个string。虽然IDE中没有错误,但在部署过程中,我收到了这个错误Error: Illegal type coercion of param LOCATION to type string[]

import { onCall } from "firebase-functions/v2/https";
import { testValidators } from "../../../services/tests";
import { defineString } from "firebase-functions/params";

export const functionName = onCall(
  {
    region: defineString("LOCATION"),
  },
  (request) => {
    testValidators(request);
  }
);


接下来,我尝试使用defineList()定义region,但在IDE中产生了一个类型错误:

import { onCall } from "firebase-functions/v2/https";
import { testValidators } from "../../../services/tests";
import { defineString } from "firebase-functions/params";

export const functionName = onCall(
  {
    region: defineList("LOCATION"),
  },
  (request) => {
    testValidators(request);
  }
);


以下是IDE错误:

Type 'ListParam' is not assignable to type 'string | string[] | Expression<string> | ResetValue | undefined'.
  Type 'ListParam' is not assignable to type 'Expression<string>'.
    The types returned by 'value()' are incompatible between these types.
      Type 'string[]' is not assignable to type 'string'.ts(2322)


下面的方法是可行的,但它是混乱的,因为region应该接受string,那么为什么我需要使用defineList()并进一步使用defineList("LOCATION") as unknown as string[]Assert它?

import { onCall } from "firebase-functions/v2/https";
import { testValidators } from "../../../services/tests";
import { defineString } from "firebase-functions/params";

export const functionName = onCall(
  {
    region: defineList("LOCATION") as unknown as string[],
  },
  (request) => {
    testValidators(request);
  }
);


总的来说,region的类型在函数和全局级别上似乎都存在很多问题。
我设法让函数级别与Assert一起工作,但我仍然更喜欢在setGlobalOptions上使用region和参数化变量。
任何帮助解决这个问题将不胜感激!

环境:

  • Node.js:16.16.0
  • firebase函数:4.4.1
  • firebase-tools:12.4.4
  • firebase-admin:11.10.1
iqjalb3h

iqjalb3h1#

根据Jed Choi提供的this answer,您必须将使用DefineList的区域设置为未知,然后将其用作region: <string[]>secret_value,

import { onCall } from "firebase-functions/v2/https";
import { testValidators } from "../../../services/tests";
import { defineString } from "firebase-functions/params";

// you must specify type as unknown explicitly
const location:unknown = defineList("LOCATION")

export const functionName = onCall(
  {
    region: <string[]>location, // you should cast as sting[]
  },
  (request) => {
    testValidators(request);
  }
);

字符串
如果您选择使用firebase函数gen1,则可以按照this answer

更新:

我测试了:您可以使用defineList全局设置setGlobalOptions区域,但您需要将其指定为字符串,以便在运行时获得分隔符,

import { setGlobalOptions } from "firebase-functions/v2/options";
import { defineList } from "firebase-functions/params";

const region = defineList("REGIONS", {
    default: ["australia-southeast1", "asia-east1"]
});

setGlobalOptions({
  region: region as unknown as string
})

qni6mghb

qni6mghb2#

我将其报告为issue,Google已经发布了一个fix,很快就会合并。

相关问题