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

Type of ObservableStatus::data should include undefined #577

Open
wmadden opened this issue Jan 12, 2023 · 1 comment
Open

Type of ObservableStatus::data should include undefined #577

wmadden opened this issue Jan 12, 2023 · 1 comment

Comments

@wmadden
Copy link

wmadden commented Jan 12, 2023

Version info

React:

Firebase:

ReactFire:
4.2.2

Other (e.g. Node, browser, operating system) (if applicable):

Test case

const myDoc = useFirestoreDoc(myRef);
console.log(myDoc.data.data().anyProperty); // <- will throw an error accessing "data.data()" since it

Steps to reproduce

Description

The Firestore access hooks, e.g. useFirestoreDoc(), useFirestoreCollection() etc, all return an ObservableStatus object. The ObservableStatus interface guarantees the presence of data, which is untrue, since it will initially be undefined (unless using suspense).

Currently, ObservableStatus is defined as follows:

export interface ObservableStatus<T> {
  status: 'loading' | 'error' | 'success';
  hasEmitted: boolean; // has received at least one value
  isComplete: boolean;
  data: T;
  error: Error | undefined;
  firstValuePromise: Promise<void>;
}

Expected behavior

const myDoc = useFirestoreDoc(myRef);
console.log(myDoc.data.data().anyProperty); // <- will throw an error accessing "data.data()" since it
//                                ^^^^^^^
// Type Error: data may be undefined

I expect the types to indicate that Observable::data is either T | undefined.

Actual behavior

The type system does not warn you when accessing properties on data while it's undefined.

At run time it throws an error attempting to access properties on undefined.

@devth
Copy link

devth commented Jun 3, 2023

Agree. I've always found the inaccurate types confusing. It leads to error prone code, since the result of these hooks is briefly undefined before they return valid values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants