Using TypeScript for Cloud Functions

For developers who prefer to write functions in TypeScript, Cloud Functions provides two types of support:

  • Create and configure TypeScript projects for automatic transpilation at initialization (firebase init functions).
  • Transpile existing TypeScript source to JavaScript at deploy time via a predeploy hook.

Following instructions in this guide, you can migrate an existing JavaScript project to TypeScript and continue deploying functions using a predeploy hook to transpile your source code. TypeScript offers many benefits over vanilla JavaScript when writing functions:

  • TypeScript supports latest JavaScript features like async/await, simplifying promise management
  • A Cloud Functions linter highlights common problems while you're coding
  • Type safety helps you avoid runtime errors in deployed functions

For a working sample of Cloud Functions written in TypeScript, see typescript-getting-started in our GitHub repo.

Initializing a new Cloud Functions project with TypeScript

Run firebase init in a new directory. The tool gives you options to build the project with JavaScript or TypeScript. Choose TypeScript to output the following project structure:

myproject
 +- functions/     # Directory containing all your functions code
      |
      +- package.json  # npm package file describing your Cloud Functions code
      |
      +- tsconfig.json
      |
      +- tslint.json # Optional file
           |
           +- src/     # Directory containing TypeScript source
               |
               +- index.ts  # main source file for your Cloud Functions code
           |
           +- lib/
               |
               +- index.js  # Built/transpiled JavaScript code
               |
               +- index.js.map # Source map for debugging

Using an existing TypeScript project

If you have an existing TypeScript project, you can add a predeploy hook to make sure your project is transpiled every time you deploy your code to Cloud Functions for Firebase. You'll need a properly formed tsconfig.json file and a Firebase project, and you'll need to make the following modifications to your Firebase configuration:

  1. Edit package.json to add a bash script to build your TypeScript project. For example:

     {
       "name": "functions",
       "scripts": {
         "build": "./node_modules/.bin/tslint -p tslint.json && ./node_modules/.bin/tsc"
       }
     ...
    
  2. Edit firebase.json to add a predeploy hook to run the build script. For example:

     {
       "functions": {
         "predeploy": "npm --prefix functions run build",
       }
     }
    

With this configuration, a firebase deploy --only functions command builds your TypeScript code and deploys it as functions.

Migrating an existing JavaScript project to TypeScript

If you have an existing Cloud Functions project that you initialized and developed in JavaScript, you can migrate it to TypeScript. You're strongly encouraged to create a git checkpoint or other backup before starting.

To migrate an existing JavaScript Cloud Functions project:

  1. Create a git checkpoint and save copies of your existing JavaScript source files.
  2. In the project directory, run firebase init functions and select TypeScript when prompted for a language for writing functions.
  3. When prompted whether to overwrite the existing package.json file, select No unless you are sure you don't want to keep the existing file.
  4. Delete index.ts in the directory functions/src, replacing it with your existing source code.
  5. In the tsconfig.json file created at initialization, set the compiler options to allow JavaScript: "allowJavaScript": "true".
  6. Copy your saved package.json file into the functions directory, and edit it to set "entrypoint" to "lib/index.js".
  7. Also in package.json, add a build script for TypeScript like the following:

     {
       "name": "functions",
       "scripts": {
         "build": "./node_modules/.bin/tslint -p tslint.json && ./node_modules/.bin/tsc"
       }
     ...
    
  8. Add "typescript" as a dev dependency by running npm install --save --only=dev typescript.

  9. For all dependencies, run npm install --save @types/<dependency>.
  10. Rewrite source code from .js to .ts as desired.

Functions logs for TypeScript projects

During firebase deploy, your project's index.ts is transpiled to index.js, meaning that the Cloud Functions log will output line numbers from the index.js file and not the code you wrote. To make it easier for you to find the corresponding paths and line numbers in index.ts, firebase deploy creates functions/lib/index.js.map. You can use this source map in your preferred IDE or via a node module.

Send feedback about...

Need help? Visit our support page.