skip to Main Content

In my project I use Cloud Functions written in "typescript": "^4.6.4" in which I’ve was about to start using satisfies feature of language but it fails to compile (weirdly enough the vscode intellisense don’t mind it at all.

Is the feature supported by Cloud Functions?

package.json:

"engines": { "node": "18" }, 
"devDependencies": { "typescript": "^4.6.4" },
"dependencies": {
  "firebase-admin": "^11.5.0",
  "firebase-functions": "^4.2.1",
// ...

tsconfig.json:

"compilerOptions": {
    "module": "commonjs",
    "noImplicitReturns": true,
    "noUnusedLocals": true,
    "outDir": "lib",
    "sourceMap": true,
    "strict": true,
    "target": "es2017",
    "baseUrl": ".",
// ...

I have an error TS1005: ',' expected. upon trying to write into Firestore document. Changing satisfies to as fixes the issue, but I suppose it should work either way.

  docRef.set({
    obj1: 0xbad,
    obj2: 0xb0b,
  } satisfies Obj1Obj2Type);

2

Answers


  1. Cloud Functions run JavaScript, not TypeScript, because TypeScript is transpiled to JavaScript.

    As a result there is no consideration to be made about what TypeScript features work or do not work in Cloud Functions — the Functions Framework has no concept of TypeScript at all in a running function because every function that runs must be pure JavaScript, whether it was written that way originally or was created by tsc before being deployed.

    If you are unable to compile your TypeScript code to JavaScript then you have a problem with your TypeScript code or configuration and that has nothing to do with Cloud Functions or the Functions Framework.

    Login or Signup to reply.
  2. The satisfies keyword was added in TypeScript in version 4.9. See the documentation: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-9.html

    The latest version is 5.0.4, so try upgrading to that if you want all of the latest that TypeScript offers as of today.

    npm install -D typescript@latest
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search