Skip to content
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

onSnapshot() results in unknown snapshot data type #4812

Closed
samtstern opened this issue Apr 21, 2021 · 2 comments · Fixed by #4817
Closed

onSnapshot() results in unknown snapshot data type #4812

samtstern opened this issue Apr 21, 2021 · 2 comments · Fixed by #4817
Assignees

Comments

@samtstern
Copy link
Contributor

[REQUIRED] Describe your environment

  • Operating System version: macOS
  • Browser version: Chrome latest
  • Firebase SDK version: 9.0.0-beta.1
  • Firebase Product: firestore

[REQUIRED] Describe the problem

Steps to reproduce:

Relevant Code:

index.ts

import { collection, getFirestore, onSnapshot } from "firebase/firestore";

function main() {
  const firestore = getFirestore();
  const colRef = "col";

  onSnapshot(
    collection(firestore, colRef),
    (snap) => {
      if (snap.empty) {
        return;
      }
      const documents = snap.docs.map((doc) => ({
        id: doc.id,
        ...doc.data(), // error: Spread types may only be created from object types
      }));
    },
    (error) => {
      console.warn(error);
    }
  );
}

In this code snap is QuerySnapshot<unknown> which makes it hard to use .data(). This can be fixed by either:

  • Casting doc.data() as any
  • Using onSnapshot<any>

Is this the intended behavior in the new SDK?

@samtstern
Copy link
Contributor Author

cc @puchesjr

@schmidt-sebastian
Copy link
Contributor

TypeScript does not retain the type of the generic class unless the type is part of the API surface. Unfortunately, CollectionReference<T> does not refer to T in any of its members. This means that we have to change the API to accommodate this. I will prepare a PR, but we need to obtain approval for this API change internally.

@schmidt-sebastian schmidt-sebastian self-assigned this Apr 21, 2021
@firebase firebase locked and limited conversation to collaborators May 28, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants