diff --git a/.changeset/silly-eagles-unite.md b/.changeset/silly-eagles-unite.md new file mode 100644 index 00000000000..4b5989200b0 --- /dev/null +++ b/.changeset/silly-eagles-unite.md @@ -0,0 +1,7 @@ +--- +"@firebase/database-compat": major +"@firebase/database-types": major +"@firebase/database": major +--- + +Updated type of action parameter for DataSnapshot#forEach diff --git a/common/api-review/database.api.md b/common/api-review/database.api.md index 8b3ef1ac7fb..4a98aea3d4e 100644 --- a/common/api-review/database.api.md +++ b/common/api-review/database.api.md @@ -33,7 +33,7 @@ export class DataSnapshot { child(path: string): DataSnapshot; exists(): boolean; exportVal(): any; - forEach(action: (child: DataSnapshot) => boolean | void): boolean; + forEach(action: (child: IteratedDataSnapshot) => boolean | void): boolean; hasChild(path: string): boolean; hasChildren(): boolean; get key(): string | null; @@ -85,6 +85,12 @@ export function goOnline(db: Database): void; // @public export function increment(delta: number): object; +// @public +export interface IteratedDataSnapshot extends DataSnapshot { + // (undocumented) + key: string; +} + // @public export function limitToFirst(limit: number): QueryConstraint; diff --git a/packages/database-compat/src/api/Reference.ts b/packages/database-compat/src/api/Reference.ts index 3f9bb63b8ff..c4846b8ee60 100644 --- a/packages/database-compat/src/api/Reference.ts +++ b/packages/database-compat/src/api/Reference.ts @@ -164,7 +164,7 @@ export class DataSnapshot implements Compat { * @returns True if forEach was canceled by action returning true for * one of the child nodes. */ - forEach(action: (snapshot: DataSnapshot) => boolean | void): boolean { + forEach(action: (snapshot: IteratedDataSnapshot) => boolean | void): boolean { validateArgCount('DataSnapshot.forEach', 1, 1, arguments.length); validateCallback('DataSnapshot.forEach', 'action', action, false); return this._delegate.forEach(expDataSnapshot => @@ -208,6 +208,13 @@ export class DataSnapshot implements Compat { } } +/** + * Represents a child snapshot of a `Reference` that is being iterated over. The key will never be undefined. + */ +export interface IteratedDataSnapshot extends DataSnapshot { + key: string; // key of the location of this snapshot. +} + export interface SnapshotCallback { (dataSnapshot: DataSnapshot, previousChildName?: string | null): unknown; } diff --git a/packages/database-types/index.d.ts b/packages/database-types/index.d.ts index 680a4a540b8..f304a4ed5b3 100644 --- a/packages/database-types/index.d.ts +++ b/packages/database-types/index.d.ts @@ -18,11 +18,18 @@ import { FirebaseApp } from '@firebase/app-types'; import { EmulatorMockTokenOptions } from '@firebase/util'; +/** + * Represents a child snapshot of a `Reference` that is being iterated over. The key will never be undefined. + */ +export interface IteratedDataSnapshot extends DataSnapshot { + key: string; // key of the location of this snapshot. +} + export interface DataSnapshot { child(path: string): DataSnapshot; exists(): boolean; exportVal(): any; - forEach(action: (a: DataSnapshot) => boolean | void): boolean; + forEach(action: (a: IteratedDataSnapshot) => boolean | void): boolean; getPriority(): string | number | null; hasChild(path: string): boolean; hasChildren(): boolean; diff --git a/packages/database/src/api.standalone.ts b/packages/database/src/api.standalone.ts index 876ec67c441..c7726143f16 100644 --- a/packages/database/src/api.standalone.ts +++ b/packages/database/src/api.standalone.ts @@ -37,6 +37,7 @@ export { OnDisconnect } from './api/OnDisconnect'; export { DataSnapshot, EventType, + IteratedDataSnapshot, QueryConstraint, QueryConstraintType, endAt, diff --git a/packages/database/src/api/Reference_impl.ts b/packages/database/src/api/Reference_impl.ts index eec1b1afafe..531c5d794da 100644 --- a/packages/database/src/api/Reference_impl.ts +++ b/packages/database/src/api/Reference_impl.ts @@ -376,7 +376,7 @@ export class DataSnapshot { } /** - * Enumerates the top-level children in the `DataSnapshot`. + * Enumerates the top-level children in the `IteratedDataSnapshot`. * * Because of the way JavaScript objects work, the ordering of data in the * JavaScript object returned by `val()` is not guaranteed to match the @@ -393,7 +393,7 @@ export class DataSnapshot { * @returns true if enumeration was canceled due to your callback returning * true. */ - forEach(action: (child: DataSnapshot) => boolean | void): boolean { + forEach(action: (child: IteratedDataSnapshot) => boolean | void): boolean { if (this._node.isLeafNode()) { return false; } @@ -463,6 +463,13 @@ export class DataSnapshot { } } +/** + * Represents a child snapshot of a `Reference` that is being iterated over. The key will never be undefined. + */ +export interface IteratedDataSnapshot extends DataSnapshot { + key: string; // key of the location of this snapshot. +} + /** * * Returns a `Reference` representing the location in the Database diff --git a/packages/firebase/compat/index.d.ts b/packages/firebase/compat/index.d.ts index f6ce4e5ea05..e4f62138d13 100644 --- a/packages/firebase/compat/index.d.ts +++ b/packages/firebase/compat/index.d.ts @@ -5819,7 +5819,7 @@ declare namespace firebase.database { * returning true. */ forEach( - action: (a: firebase.database.DataSnapshot) => boolean | void + action: (a: firebase.database.IteratorDataSnapshot) => boolean | void ): boolean; /** * Gets the priority value of the data in this `DataSnapshot`.