Skip to content

rtdb(types): DataSnapshot#forEach - action parameter has null key #6368

@rhodgkins

Description

@rhodgkins

[REQUIRED] Describe your environment

  • Operating System version: N/A
  • Browser version: N/A
  • Firebase SDK version: 9.8
  • Firebase Product: database

[REQUIRED] Describe the problem

When using DataSnapshot#forEach the parameter available to the action closure is a DataSnapshot which is correct, but the key property of this value has the type string | null, but as its a child of the iterating snapshot it can't be the root node so its key can never be null.

Steps to reproduce:

const node = await getDatabase().ref().get()
node.forEach(child => {
   const key = child.key
   // key is possibly null so have to cast etc.
})

Relevant Code:

forEach(action: (a: DataSnapshot) => boolean | void): boolean;

forEach(
action: (a: firebase.database.DataSnapshot) => boolean | void
): boolean;

forEach(action: (snapshot: DataSnapshot) => boolean | void): boolean {

Fix

Happy to do a PR for this - I just remember it being a bit complicated last time I did this. Is the info in that issue still correct - the function signature in the above 3 places just needs to be changed?

Would changing the function signature to forEach(action: (a: DataSnapshot & { key: string }) => boolean | void): boolean; be OK for a fix?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions