Skip to content

Firebase typings not compatible with strictFunctionTypes in TypeScript 2.6 #291

Closed
@cartant

Description

@cartant

[REQUIRED] Describe your environment

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

[REQUIRED] Describe the problem

The callback typings for on and off are not consistent and effect errors when using TypeScript 2.6 and strictFunctionTypes.

With the code in this issue (in the Relevant Code section), this error is effected:

TSError: ⨯ Unable to compile TypeScript
index.ts (4,24): Argument of type '(a: DataSnapshot | null, b?: string | undefined) => any' is not assignable to parameter of type '((a: DataSnapshot, b?: string | null | undefined) => any) | undefined'.
  Type '(a: DataSnapshot | null, b?: string | undefined) => any' is not assignable to 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'. (2345)
    at getOutput (...\node_modules\ts-node\src\index.ts:307:15)
    at ...\node_modules\ts-node\src\index.ts:336:16
    at Object.compile (...\node_modules\ts-node\src\index.ts:498:11)
    at Module.m._compile (...\node_modules\ts-node\src\index.ts:392:43)
    at Module._extensions..js (module.js:635:10)
    at Object.require.extensions.(anonymous function) [as .ts] (...\node_modules\ts-node\src\index.ts:395:12)
    at Module.load (module.js:545:32)
    at tryModuleLoad (module.js:508:12)
    at Function.Module._load (module.js:500:3)
    at Function.Module.runMain (module.js:665:10)

The problem is that the on method returns a callback with a signature that differs from that of the callback the off method accepts.

In particular, it's the type of the optional b parameter. In the callback returned by on its type is b?: string, but in the callback accepted by off its type is b?: string | null.

Steps to reproduce:

Compile the code included below using TypeScript 2.6.1, with the strictFunctionTypes option enabled or with the strict option set to true.

Relevant Code:

import * as firebase from "firebase";
function bug(query: firebase.database.Query): void {
    const listener = query.on("value", (snapshot: firebase.database.DataSnapshot | null) => {});
    query.off("value", listener);
}

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions