Alwaysblue
Alwaysblue

Reputation: 11830

Writing type for firebase functions/storgae/database in interface

What would be the typescript types for firebase functions/storage/admin?

So I am new to typescript, and was updating my code JS code to typescript.

In my code, I am creating context obj

const context = {
    functions: functions,
    realtime: admin.database(),
    firestore: admin.firestore()
    admin: admin
}

Now, I am passing this context and I want to create interface for it? Can someone help me in figuring out the interface for the above? And in-general, How do you determine types of something?

Upvotes: 1

Views: 92

Answers (2)

hackape
hackape

Reputation: 19947

I believe TS is able to infer the type of context object automatically, with typeof keyword.

// keep the code as-is:
const context = {
    functions: functions,
    realtime: admin.database(),
    firestore: admin.firestore()
    admin: admin
}

// then add:
type IContext = typeof context

You can safely pass this interface around. No need to manually declare it unless you need to tailor-made it to meet specific requirement.

Upvotes: 1

hoangdv
hoangdv

Reputation: 16127

Just define your interface base on property types. The firestore is admin.firestore.Firestore interface, the same for each other.

This is my example with firebase admin (I don't know what is the functions):

import firebaseAdmin from 'firebase-admin';
import * as admin from 'firebase-admin';

interface IContext {
  realtime: admin.database.Database;
  firestore: admin.firestore.Firestore;
  admin: typeof firebaseAdmin;
}

// then
const context: IContext = {
  realtime: admin.database(),
  firestore: admin.firestore(),
  admin,
};

Upvotes: 1

Related Questions