Writing Cloud Functions with Typescript

Writing Cloud Functions with Typescript

Serverless architectures are awesome and I recently started to use Cloud Functions (via Firebase). Immediately, I looked into using Typescript to write my functions. There are mainly two reasons for that: 1) my projects are written in Typescript it would be useful to use some of these files in my functions; 2) out of the box, Cloud Functions provides typings for Typescript, lowering the barrier to entry this new API.

First, created a functions folder within my own project with the following package.json.

{
  "name": "myproject-functions",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "watch": "tsc --watch",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "firebase-admin": "^4.2.0",
    "firebase-functions": "^0.5.4",
    "lodash": "^4.17.4"
  },
  "devDependencies": {
    "typescript": "^2.2.2"
  }
}

We also need to add a tsconfig.json file into the functions folder. In the package.json of the root project, You can update your lint script to also check the functions folder. In my projects, it looks like this:

tslint --project tsconfig.json && tslint --project functions/tsconfig.json

TypeRoots

By default, Typescript will look into parent node_modules folders to resolve types which can cause quite some confusion when compiling. To avoid this chaos, there is a compiler option called typeRoots that allows you to specify the root type folder. You can set it in your tsconfig.json under the compilerOptions sections as:

"typeRoots":["node_modules/@types"]

Imports

If you use es6 module imports in your functions, you will need to do some modifications to your code. This is because Cloud functions wraps the JS code into a function invocation. Consider the function below.

import functions from "firebase-functions";
import {Request, Response} from "express";

exports.myFunctions = functions.https.onRequest((req, res) => {
  //...
})

At deployment, you will get the error below.

You can fix it by using require instead of import. If you only import types, you can still use import since these declarations won’t be present in the generated JS file.

const functions = require("firebase-functions");
// If we only import types, we can use import.
import {Request, Response} from "express";

exports.myFunctions = functions.https.onRequest((req, res) => {
  //...
})

Et voilà

I hope this article was helpful to you, and I look forward to receiving your feedback.

Justin Christensen

Sr. Software Engineer at Enzy

7 年

the typeRoots thing was killing me. Thanks for this. I have been looking all over for a solution to the thousands of lines of warnings from typescript.

回复

要查看或添加评论,请登录

William Candillon的更多文章

  • React Native Gestures & Animations: you only have 16ms to render everything

    React Native Gestures & Animations: you only have 16ms to render everything

    The default Gesture and Animation APIs from React Native have some small declarative parts but they are mostly…

    1 条评论
  • "Can it be done in React Native?"?-?a case for declarative gestures and animations

    "Can it be done in React Native?"?-?a case for declarative gestures and animations

    Not long after starting React Native development, I started to look at the apps on my phone in a very different way…

  • Building a Product Live on YouTube

    Building a Product Live on YouTube

    Recently, I created a premium iOS and Android starter kit named Elements. The design is from Sketch Elements and the…

  • React Native Do with Firebase Integration

    React Native Do with Firebase Integration

    Since its release, the React Native Do starter kit has gained substantial traction and it was time to provide new…

  • Responsive UIs in React Native

    Responsive UIs in React Native

    Recently, I published a premium starter kit for RN named React Native DO. Like many React Native apps on the market…

  • The 80/20 of React Native

    The 80/20 of React Native

    When building a React Native app, what twenty percent of your effort brings eighty percent of the result? After…

  • Building a Fitness App with React Native

    Building a Fitness App with React Native

    I am an avid user of apps like Freeletics and Headspace which target the fitness of the body and mind. These apps have…

  • React Native Push Notifications with Firebase

    React Native Push Notifications with Firebase

    Last week, Swizec Teller wrote an fantastic tutorial on adding push notifications to your React Native app using Cloud…

    1 条评论
  • Firebase Schema Evolution

    Firebase Schema Evolution

    Schema evolution is a natural part of your application lifecycle. Firebase is my go-to backend for web and mobile…

  • Image Pipeline with React Native ListView

    Image Pipeline with React Native ListView

    In mobile apps, scrolling through a list of images is a very common use case for which users have high expectations. In…

社区洞察

其他会员也浏览了