Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type Mismatch Problems While Setting region Property for Gen 2 Firebase Functions using TypeScript and a Parameterized Variable #6204

Closed
BenJackGill opened this issue Jul 18, 2023 · 2 comments · Fixed by #6205

Comments

@BenJackGill
Copy link

BenJackGill commented Jul 18, 2023

Issue Description:

I am having trouble setting the region property of my Gen 2 Firebase Functions using TypeScript and a parameterized variable due to type mismatch issues. Here's a sample of my .env file:

LOCATION="australia-southeast1"

I have attempted to define the region both globally and at the function level.

1. Defining region globally using setGlobalOptions

I would prefer to define the region globally using setGlobalOptions.

The global region property on setGlobalOptions has a type of GlobalOptions.region?: string | Expression<string> | ResetValue | undefined. This is slightly different from the function level type HttpsOptions.region?: string | string[] | Expression<string> | ResetValue | undefined as the global level only allows for a singular string and no string[].

No matter what I do at the global level, it fails. All three of these attempts give a deployment error Error: Illegal type coercion of param LOCATION to type string[]:

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

Therefore, I am unsure how to define region on setGlobalOptions using a parameterized variable.

2. Defining region at the function level

When I tried defining region at the function level, the IDE showed region as HttpsOptions.region?: string | string[] | Expression<string> | ResetValue | undefined.

I thought defineString() would work as it returns a string. Although there were no errors in the IDE, during deployment I received this error 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);
  }
);

Next, I tried to define the region using defineList(), but that produced a type error in the 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);
  }
);

Here is the IDE error:

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)

The following works, but it is confusing because region should accept a string, so why do I need to use defineList() and further assert it with defineList("LOCATION") as unknown as string[]?

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

Overall there seems to be a lot of problems with the typing for region both at the function and global level.

I managed to get the function level working with an assertion, but I would still prefer to use region on setGlobalOptions with a parameterized variable.

Any help to resolve this issue would be greatly appreciated!

Environment:

  • Node.js: 16.16.0
  • firebase-functions: 4.4.1
  • firebase-tools: 12.4.4
  • firebase-admin: 11.10.1
@google-oss-bot
Copy link
Contributor

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@blidd-google
Copy link
Contributor

Hey @BenJackGill, thanks for pointing this issue out. We have a fix out at #6205, please stay tuned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy