Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Import FieldValue from firebase-admin using Typescript?

Is it possible to import FieldValue from firebase-admin npm using this style?

import { FieldValue } from 'firebase-admin/firestore';

By default my visual studio Code imports in this way:

import {FieldValue} from '@google-cloud/firestore';

but in this way, I have to add a new package only to import FieldValue ...

Is there any way to import it from firebase-admin using Typescript?

like image 689
Ing. Alejandro Villalón Avatar asked Jan 03 '19 00:01

Ing. Alejandro Villalón


People also ask

How do I write a function in typescript in Firebase?

In the project directory, run firebase init functions and select TypeScript when prompted for a language for writing functions. 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.

How do I add type definitions to Firebase?

From what I've read, Firebase can use the "Definitely Typed" library to serve type definitions through "@types/firebase". However, if you're using npm to load Firebase (such as with a Webpack-based build process), you can use the type definitions that ship directly with the Firebase module.

How do I export an interface from Firebase?

The Firebase module doesn't export interfaces - it exports a single object that can be imported using the import-require syntax in TypeScript. Using this single object, you can then access the type definitions using a series of namespaces.

How are my Firebase variables being declared?

As you can see, each of my variables is being declared with a type definition that is provided by the firebase module. The namespacing makes the declarations fairly verbose, which is why it might be wise to wrap your Firebase implementation in a Gateway / Repository class that can provide easier type inference.


1 Answers

If you import like this:

import { firestore } from 'firebase-admin'

You can find it like this:

firestore.FieldValue

as it's re-exported from @google-cloud/firstore as part of its exported admin.firestore namespace.

like image 150
Doug Stevenson Avatar answered Sep 17 '22 18:09

Doug Stevenson