sukh
sukh

Reputation: 358

Flow: Using custom array types in function response

I'm using Firebase Firestore queries. I've defined a custom object type (TodoItem) which I want in the response which I'd expect to be in an array collection (TodoCollection).

I'm having issues getting this flow typed though. Below is my code, where by I'm getting the following error within the onCollectionUpdate function:

[flow] function (This type is incompatible with TodoCollection)

type TodoItem = {
  key: ?string,
  title: ?string,
  foo: ?string,
}

type TodoCollection = $ReadOnlyArray<TodoItem>

const onCollectionUpdate: TodoCollection = (querySnapshot: QuerySnapshot) => {
  const collection = []

  querySnapshot.forEach((doc: DocumentSnapshot) => {
    const { title, foo } = doc.data()
    const entry: TodoItem = {
      key: doc.id,
      // doc, // DocumentSnapshot
      title,
      foo,
    }

    collection.push(entry)
  })

  return collection
}

The array ideally could be empty which is whats throwing me off a little. Any help / guidance would be appreciated.

Upvotes: 0

Views: 45

Answers (1)

SLaks
SLaks

Reputation: 887449

const onCollectionUpdate: TodoCollection 

You just declared a variable of type TodoCollection, which is an array.

You're assigning it a function, which is not an array, so you get an error.

Upvotes: 3

Related Questions