我已更新到firebase模块的4.12.1版本
我正在使用typescript并导入模块,如下所示
import * as firebase from 'firebase'; import * as functions from 'firebase/functions';
firebase.functions().httpsCallable('myFunc')(payload)
这导致编译器错误:
Property ‘functions’ does not exist on type ‘typeof firebase’.
根据文档,它们应该如何被称为:https://firebase.google.com/docs/functions/callable
如果我用常规导入替换导入一切正常
const firebase = require('firebase'); const functions = require('firebase/functions');
类型定义是否存在已知问题,或者我在这里做错了什么?
解决方法
看起来firebase JS SDK的当前版本4.12.1中的@firebase / *类型包存在问题.它们根本不包含在node_modules / firebase / app / index.d.ts和node_modules / firebase / index.d.ts中.似乎有一个branch “build-fixes” that target that issue.
更新:分支已合并,但无法修复.我在这里添加了一个专用的pull请求:最近合并的https://github.com/firebase/firebase-js-sdk/pull/724,期望下一个版本中的更改> 4.13.1
我通过将https://github.com/mpoehler/firebase-js-sdk/blob/master/packages/functions-types/index.d.ts中的Type声明添加到node_modules / firebase / app / index.d.ts来修复它.这样做我可以使用以下代码,没有错误,并在VSC中键入建议.
import * as firebase from 'firebase/app'; import '@firebase/functions'; firebase.initializeApp({ ... }); // call function let func = firebase.functions().httpsCallable('myFunc'); func({text: 'huch'}).then(result => console.log('...and returned!'));
这是一个肮脏的黑客而不是解决方案,我希望上面提到的PR很快就会合并.
根据@jbb的要求,以下是node_modules / firebase / app / index.d.ts中需要的重要部分:
declare namespace firebase { .... function functions(app?: firebase.app.App): firebase.functions.FirebaseFunctions; ... } declare namespace firebase.app { interface App { ... functions(): firebase.functions.FirebaseFunctions; ... } } declare namespace firebase.functions { export interface HttpsCallableResult { readonly data: any; } export interface HttpsCallable { (data?: any): Promise<HttpsCallableResult>; } export class FirebaseFunctions { private constructor(); httpsCallable(name: string): HttpsCallable; } export type FunctionsErrorCode = | 'ok' | 'cancelled' | 'unknown' | 'invalid-argument' | 'deadline-exceeded' | 'not-found' | 'already-exists' | 'permission-denied' | 'resource-exhausted' | 'Failed-precondition' | 'aborted' | 'out-of-range' | 'unimplemented' | 'internal' | 'unavailable' | 'data-loss' | 'unauthenticated'; export interface HttpsError extends Error { readonly code: FunctionsErrorCode; readonly details?: any; } }