This is an example Firebase project for using TypeScript with Cloud Functions for Firebase
TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.
One of the biggest challenges with developing in JavaScript is that it is very easy to write code that has runtime errors. TypeScript enables the fast development of JavaScript with optional types. When types are used, supported editors provide auto-suggest for methods and properties along with syntax highlighting of errors, which speeds development.
TypeScript supports targeting different browsers, and optimizes the resulting JavaScript. It is much easier to write clean, consistent code across a project and development team. TypeScript offers support for the latest and evolving JavaScript features like async functions and decorators, to help build robust components.
For a nice intro to TypeScript, check out the TypeScript PlayGround.
The TypeScript source is in functions/src
and then we need to do a build
step before deploying (see steps below). The main Cloud Function entry
point is src/index.ts
which compiled to src/index.js
and that is specified
in functions/package.json
.
There are two key differences to the example Cloud Function:
require
->import
exports.
->export let
JavaScript:
var functions = require('firebase-functions');
exports.helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!\n\n");
});
TypeScript:
import * as functions from 'firebase-functions'
export let helloWorld = functions.https.onRequest((request, response) => {
response.send("Hello from Firebase!\n\n");
});
This project was set up using yarn which will ensure that your deployed Cloud Functions are using the exact same versions of npm modules that you are running locally. The specific version of each dependency is saved in yarn.lock
This example has the default sample function. To use this example as a starter project:
npm install -g firebase-tools
git clone https://github.com/firebase/functions-samples.git
- Create a Firebase Project using the Firebase Developer Console
- Make a directory for your project and cd into it. Then initialize
the Firebase project and replace the default functions directory with
this one. The directory name doesn't have to be the same as your
Firebase project name -- that's just common practice. In the following
steps a shell variable is used to make it simple to change the first line:
PROJECT=your-project-name mkdir $PROJECT; cd $PROJECT firebase init firebase use --add $PROJECT rm -rf functions cp -r ../functions-samples/typescript-getting-started/functions functions
- Install the dependencies and deploy
cd functions yarn # or npm install npm run deploy
Note: with TypeScript you need to build the JavaScript files before deploying, so there's an npm script that does the steps. You can see that and a few other handy shortcuts in package.json
After the deploy is complete, you will see output with the URL of your
Cloud Function endpoint. You can test the function with curl. The following
command will work with any project, since the output of firebase use
is
the current project ID:
curl https://us-central1-$(firebase use).cloudfunctions.net/helloWorld