Description
[REQUIRED] Describe your environment
- Operating System version: N/A
- Browser version: N/A
- Firebase SDK version: 5.4.2
- Firebase Product: database (auth, database, storage, etc)
[REQUIRED] Describe the problem
Steps to reproduce:
.off
is supposed to be able to take a function that has been used with .on
. However, pass a function used with .on
to .off
and it throws a type error. If I had to hazard a guess, I would guess that this is because the type definitions of the callback functions that .on
and .off
accept are completely different.
Relevant Code:
import firebase from "firebase";
const onFn = firebase
.database()
.ref("foo")
.on("value", snapshot => snapshot);
firebase
.database()
.ref("foo")
.off("value", onFn);
This produces an error, complaining about the onFn
in the last line:
error TS2345: Argument of type '(a: DataSnapshot | null, b?: string | undefined) => any' is not assignable to parameter of type '(a: DataSnapshot, b?: string | null | undefined) => any'.
Types of parameters 'b' and 'b' are incompatible.
Type 'string | null | undefined' is not assignable to type 'string | undefined'.
Type 'null' is not assignable to type 'string | undefined'.
Even if trying to specify types through explicit type declarations, or including the optional second parameter in the callback function, etc. it still produces varying errors to do with the types not matching:
on(
eventType: EventType,
callback: (a: firebase.database.DataSnapshot | null, b?: string) => any,
cancelCallbackOrContext?: Object | null,
context?: Object | null
): (a: firebase.database.DataSnapshot | null, b?: string) => any;
off(
eventType?: EventType,
callback?: (a: firebase.database.DataSnapshot, b?: string | null) => any,
context?: Object | null
): any;
Specifically notice the lack of | null
in the first argument in .off
, and simultaneously the omission of | null
in the second argument in .on
(even though the docs say it may be passed null
).
Furthermore, the documentation for .on
seems to imply that the callback will always be passed a DataSnapshot ("The callback will be passed a DataSnapshot."), so having | null
in the type definition seems out of step with the documentation.
Also, fun fact: the actual source file for Query defines the callback argument in both methods as being of the SnapshotCallback
type. This type, as defined in the source file, matches neither of the definitions of .on
or .off
in the .d.ts
files:
export interface SnapshotCallback {
(a: DataSnapshot, b?: string): any;
}