Search code examples
typescriptfirebasegoogle-cloud-functions

Firebase functions imports in firebase functions 6.0.1


In firebase-functions 3.13.2 I was able to import using typescript:

import * as functions from 'firebase-functions';
import { UserRecord } from 'firebase-functions/lib/providers/auth';

These import statements do not work with firebase-functions 6.0.1

I get these errors:

error TS2307: Cannot find module 'firebase-functions' or its corresponding type declarations.

import * as functions from 'firebase-functions';

error TS2307: Cannot find module 'firebase-functions/lib/providers/auth' or its corresponding type declarations.

import { UserRecord } from 'firebase-functions/lib/providers/auth';

There are examples using require e.g. const functions = require("firebase-functions/v1");

Is it possible to use import with firebase-functions 6.0.1?

How do I import the type definition of UserRecord from firebase-functions 6.0.1?


Solution

  • Firstly, import * as functions from 'firebase-functions' should compile just fine. It does for me. If it doesn't for you, maybe there is something wrong with your setup. But you probably don't want to use the wildcard import given the way that v1 and v2 functions differ in what they require to import.

    Secondly, you can import UserRecord like this:

    import { UserRecord } from "firebase-functions/lib/common/providers/identity"
    

    For example, if you're trying to write an onCreate auth function, this compiles for me:

    import { auth } from "firebase-functions/v1"
    import { UserRecord } from "firebase-functions/lib/common/providers/identity"
    
    export const functionName = auth.user().onCreate((user: UserRecord) => {
        // ...
    })