Skip to content

Add an interface Database #3511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/thirty-flies-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@firebase/database-types": patch
"@firebase/database": patch
---

Added interface `Database` which is implemented by `FirebaseDatabase. This allows consumer SDKs (such as the Firebase Admin SDK) to export the database types as an interface.
11 changes: 9 additions & 2 deletions packages/database-types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ export interface DataSnapshot {
val(): any;
}

export class FirebaseDatabase {
private constructor();
export interface Database {
app: FirebaseApp;
goOffline(): void;
goOnline(): void;
ref(path?: string | Reference): Reference;
refFromURL(url: string): Reference;
}

export class FirebaseDatabase implements Database {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a .d.ts file, isn't "class" already considered an interface? Could we just drop private constructor?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC, we use it to type the actual class that we export and we don't want people to instantiate it, so private constructor is important. Please correct me if i'm wrong.

Copy link
Member Author

@Feiyang1 Feiyang1 Jul 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use d.ts file to annotate a JS library. It tells Typescript that the JS library exports a class that people can import and use. People can actually do this and Typescript allows it:

import {FirebaseDatabase} from '@firebase/database-types';

new FirebaseDatabase(); // if not having the private constructor

For this reason, I consider it as a bad practice to use class instead of interface in our types packages, if you are just going for the instance type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understand this correctly, it seems like the following two are equivalent:

export class Foo {
  private constructor();
  foo: string;
}

export interface Foo {
 foo: string;
}

If so, can we drop the existing class declaration? If not, this LGTM.

Copy link
Member Author

@Feiyang1 Feiyang1 Jul 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are the same for representing the instance type, but we need the constructor type which requires a class type. See https://github.com/firebase/firebase-js-sdk/blob/master/packages/database/index.ts#L97

I believe it is to allow people to do instanceof, but disallow newing an object, so we need to keep the class type.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the instanceof check was the only thing that I could think of, but it seemed a bit unlikely. Better not to break anyone though.

private constructor();
app: FirebaseApp;
goOffline(): void;
goOnline(): void;
Expand Down
2 changes: 1 addition & 1 deletion packages/database/index.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export function initStandalone(app: FirebaseApp, url: string, version: string) {
app,
authProvider,
url
) as types.FirebaseDatabase,
) as types.Database,
namespace: {
Reference,
Query,
Expand Down