I am using firebase cloud functions with javascript on cloud functions. And, I want to switch from javascript to typescript.
However I cannot use firebase-admin on typescript as following command failed.
command: npm install @types/firebase-admin --save-dev
error: '@types/firebase-admin' is not in the npm registry.
According to this release note, it looks that firebase admin support typescript. Can somebody tell us how to use typescript with firebase-admin on cloud functions?
https://firebase.google.com/support/release-notes/admin/node#4.1.3
Using an existing TypeScript project With this configuration, a firebase deploy --only functions command builds your TypeScript code and deploys it as functions.
Since Cloud Functions run on the Node. js runtime, you need to write Cloud Functions in JavaScript (or a language that can be compiled to JavaScript). And if you are writing Cloud Functions for Firebase, then the only languages that are officially supported are JavaScript and TypeScript. Note: Node.
onRequest creates a standard API endpoint, and you'll use whatever methods your client-side code normally uses to make. HTTP requests to interact with them. onCall creates a callable. Once you get used to them, onCall is less effort to write, but you don't have all the flexibility you might be used to.
You should use Cloud Functions for Firebase if you're a developer building a mobile app or mobile web app. Firebase gives mobile developers access to a complete range of fully managed mobile-centric services including analytics, authentication and Realtime Database.
Another option could be this way.
import * as admin from 'firebase-admin';
import * as serviceAccount from './service-account.json';
const firebaseAdmin = admin.initializeApp({
credential: admin.credential.cert(serviceAccount as admin.ServiceAccount)
});
You don't need to install an @types
module, because firebase-admin
ships with TypeScript support in the box. You should be able to use it with TypeScript just by installing firebase-admin
.
import * as admin from 'firebase-admin';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With