The following steps show how to set up TypeScript yourself starting from scratch.
Steps:
- Create a project in the Firebase Console
in this example, I called mine
functions-typescript
- Create a directory for your project and do the following steps inside that
directory, for example:
mkdir firebase-functions-typescript cd firebase-functions-typescript
- Then to set up the firebase project
firebase init
- You will be prompted to select the Firebase project you just created in the Console UI and choose which Firebase features you want to use. Select Functions and whatever other features you want to use. Choose Typescript as the language you want to use for your functions and then type "n" when prompted to install dependencies with npm.
- The project will be automatically created with a ready-to-use Typescript enabled project. Your source code will go into the
functions/src
directory and it will be compiled to thefunctions/lib
directory when running the Typescript compiler. - To compile your source code you can run
npm run build
from the functions directory. Also note that a pre-deploy trigger has been added so that your source code is automatically compiled before each deployment when runningfirebase deploy
. - install dependencies with npm (still in
functions
directory)
npm install
Note: if you want to just start using TypeScript gradually, you can target "es5" and the compiler will allow you to intermix old-school JavaScript and TypeScript.
- I like to exclude node_modules and compiled js files from git, so
I add this to a root level
.gitignore
file
node_modules/
## ignore generated JavaScript files
functions/**/*.js
functions/**/*.js.map
- in
functions
directory, use the npm deploy script to deploy your functions:
npm run deploy
You will see a bunch of output and at the end it will show you the URL for your deployed function.
- test YOUR function with curl. For mine I can do this:
curl https://us-central1-functions-typescript.cloudfunctions.net/helloWorld
Now start developing in TypeScript!
Initial steps were based on nice article by @wcandillon