Skip to content

firestore: modular doc method returns wrong type (typescript) #5572

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

Closed
thdk opened this issue Oct 4, 2021 · 2 comments
Closed

firestore: modular doc method returns wrong type (typescript) #5572

thdk opened this issue Oct 4, 2021 · 2 comments

Comments

@thdk
Copy link

thdk commented Oct 4, 2021

[REQUIRED] Describe your environment

  • Operating System version: Linux desktop 5.11.0-36-generic 40~20.04.1-Ubuntu SMP x86_64 GNU/Linux

  • Browser version: n.a.

  • Firebase SDK version: 9.1.1

  • Firebase Product: firestore

[REQUIRED] Describe the problem

Steps to reproduce:

Compile the typescript code below using firebase 9.1.1

Relevant Code:

import {
    doc,
} from "firebase/firestore";

import * as types from "@firebase/firestore-types";

import { initializeTestEnvironment } from "@firebase/rules-unit-testing";

initializeTestEnvironment({})
   .then((testEnv) => {
       const firestore = testEnv.unauthenticatedContext().firestore();

       // error on below line: Type 'DocumentReference<DocumentData>' is missing the following properties from type 'DocumentReference<DocumentData>': collection, isEqual, set, update, and 3 more.ts(2740)
       const docRef: types.DocumentReference = doc(firestore, "books/foo");
   });

When I navigate to the typescript declaration file for doc I see the following signatures:

    function doc(firestore: types.FirebaseFirestore, path: string, ...pathSegments: string[]): DocumentReference<DocumentData>;
    function doc<T>(reference: types.CollectionReference<T>, path?: string, ...pathSegments: string[]): DocumentReference<T>;
    function doc(reference: types.DocumentReference<unknown>, path: string, ...pathSegments: string[]): DocumentReference<DocumentData>;

As you can see the return type in the declaration file is DocumentReference which gets imported from @firebase/firestore while in other places in the same declaration file types.DocumentReference is used where types is imported from @firebase/firestore-types.

I would think that the types from @firebase/firestore-types should be compatible with those in firebase/firestore.

@Feiyang1
Copy link
Member

Feiyang1 commented Oct 4, 2021

@firebase/firestore-types are compat types. They are compatible with firebase/firestore in the sense that they are accepted in functions exported from firebase/firestore, but the types from the 2 packages are not compatible(you can't assign them to each other). Can you use DocumentReference from firebase/firestore instead?

@thdk
Copy link
Author

thdk commented Oct 5, 2021

Hi thank you for your reply.

I have change my code now back to using DocumentReference from firebase/firestore. The reason I had started using @firebase/firestore-types package for my types was this comment here #5550 (comment).

I only needed that one FirebaseFirestore type but got too enthusiast and started using CollectionReference and DocumentReference as well.

@firebase firebase locked and limited conversation to collaborators Nov 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants