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

Add an interface Database #3511

merged 4 commits into from
Jul 30, 2020

Conversation

Feiyang1
Copy link
Member

The admin SDK wants to export the type of the instance of the FirebaseDatabase class in their namespace, but encountered an issue where Typescript thinks they are exporting the actual class from @firebase/database-types and compiles it to code that leads to runtime error:

var database_types_1 = require("@firebase/database-types");

Adding an interface that's implemented by FirebaseDatabase to work around the issue.

I tried to use InstanceType<typeof FirebaseDatabase> to get the type of the instance without creating a new type, but it doesn't work because the constructor is private 🤷 .

@changeset-bot
Copy link

changeset-bot bot commented Jul 29, 2020

🦋 Changeset is good to go

Latest commit: 12f8c00

We got this.

This PR includes changesets to release 9 packages
Name Type
@firebase/database-types Patch
@firebase/database Patch
firebase Patch
@firebase/testing Patch
firebase-browserify-test Patch
firebase-package-typings-test Patch
firebase-messaging-selenium-test Patch
firebase-typescript-test Patch
firebase-webpack-test Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

@hiranya911 hiranya911 left a comment

Choose a reason for hiding this comment

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

Thanks. LGTM!

@MathBunny FYA.

@google-oss-bot
Copy link
Contributor

google-oss-bot commented Jul 29, 2020

Binary Size Report

Affected SDKs

  • @firebase/firestore

    Type Base (fc71940) Head (44b322b) Diff
    browser 246 kB 247 kB +1.05 kB (+0.4%)
    esm2017 193 kB 193 kB +417 B (+0.2%)
    main 470 kB 471 kB +1.95 kB (+0.4%)
    module 243 kB 244 kB +960 B (+0.4%)
    react-native 193 kB 193 kB +417 B (+0.2%)
  • @firebase/firestore/exp

    Type Base (fc71940) Head (44b322b) Diff
    browser 187 kB 187 kB +403 B (+0.2%)
    main 466 kB 467 kB +1.93 kB (+0.4%)
    module 187 kB 187 kB +403 B (+0.2%)
    react-native 187 kB 187 kB +403 B (+0.2%)
  • @firebase/firestore/memory

    Type Base (fc71940) Head (44b322b) Diff
    browser 184 kB 185 kB +1.05 kB (+0.6%)
    esm2017 144 kB 145 kB +417 B (+0.3%)
    main 345 kB 347 kB +1.95 kB (+0.6%)
    module 182 kB 183 kB +960 B (+0.5%)
    react-native 145 kB 145 kB +417 B (+0.3%)
  • firebase

    Type Base (fc71940) Head (44b322b) Diff
    firebase-firestore.js 284 kB 285 kB +948 B (+0.3%)
    firebase-firestore.memory.js 225 kB 226 kB +948 B (+0.4%)
    firebase.js 818 kB 819 kB +956 B (+0.1%)

Test Logs


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.

@Feiyang1 Feiyang1 merged commit ef348fe into master Jul 30, 2020
@Feiyang1 Feiyang1 deleted the fei-class-interface branch July 30, 2020 18:47
@google-oss-bot google-oss-bot mentioned this pull request Aug 4, 2020
@firebase firebase locked and limited conversation to collaborators Aug 30, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants