NodeJS 如何定制firebase DocumentData界面?

bvjxkvbb  于 2023-01-16  发布在  Node.js
关注(0)|答案(3)|浏览(106)

我使用的是firebase@5.5.8和类型脚本@3.1.4
每当我从firestore创建文档时,我都会得到一个firebase.firestore.DocumentReference类型的对象
如果我调用get(options?: firebase.firestore.GetOptions|undefined),我将得到一个firebase.firestore.DocumentSnapshot类型的对象
如果我调用data(options?: firebase.firestore.DataOptions|undefined),我将得到一个firebase. firestore. DocumentData对象或未定义,正如预期的那样
现在,在我的管理器对象上,我知道我要向数据库写入什么,所以我可以Assert,无论您从管理器中得到什么DocumentData,都将得到一个Client,如下所示

export interface Client {
    name: string;
    website?: string;
    description?: string;
    visible: boolean;
}

我想为我的管理器对象创建一个接口来表达这一点,所以我尝试了:

export interface FirebaseDocumentSnapshot<T> extends $firebase.firestore.DocumentSnapshot {
    data(options?: $firebase.firestore.SnapshotOptions | undefined): T|undefined
}

export interface FirebaseDocumentReference<T> extends $firebase.firestore.DocumentReference {
    get(options?: $firebase.firestore.GetOptions | undefined): Promise<FirebaseDocumentSnapshot<T>>
}

我的问题是:

const client: Client = mapClient(args);

const result: FirebaseDocumentReference<Client> = await $db.add(client);

错误为:
[ts]

Type 'DocumentReference' is not assignable to type 'FirebaseDocumentReference<Client>'.
  Types of property 'get' are incompatible.
    Type '(options?: GetOptions | undefined) => Promise<DocumentSnapshot>' is not assignable to type '(options?: GetOptions | undefined) => Promise<FirebaseDocumentSnapshot<Client>>'.
      Type 'Promise<DocumentSnapshot>' is not assignable to type 'Promise<FirebaseDocumentSnapshot<Client>>'.
        Type 'DocumentSnapshot' is not assignable to type 'FirebaseDocumentSnapshot<Client>'.
          Types of property 'data' are incompatible.
            Type '(options?: SnapshotOptions | undefined) => DocumentData | undefined' is not assignable to type '(options?: SnapshotOptions | undefined) => Client | undefined'.
              Type 'DocumentData | undefined' is not assignable to type 'Client | undefined'.
                Type 'DocumentData' is not assignable to type 'Client'.
                  Property 'name' is missing in type 'DocumentData'. [2322]
const result: FirebaseDocumentReference<Client>

如何声明接口以便知道结果对象的类型?

rekjcdws

rekjcdws1#

export declare function getDocs<T>(query: Query<T>): Promise<QuerySnapshot<T>>;
wyyhbhjk

wyyhbhjk2#

一个简单的解决方案是强制转换返回数据:
const client = snapshot.data() as Client

sbtkgmzw

sbtkgmzw3#

@gal-bracha的解决方案可以“工作”,因为TypeScript编译器会假设client的类型为Client,但如果错误数据最终出现在Firestore中,则无法防止运行时错误。处理应用外部数据时,更安全的解决方案是使用类似Yup的方法显式验证数据:

import * as yup from "yup";

const clientSchema = yup.object({
  name: yup.string(),
  website: yup.string().url().notRequired(),
  description: yup.string().notRequired(),
  visible: yup.boolean()
});
// You can use this elsewhere in your app
export type Client = yup.InferType<typeof clientSchema>;

// Usage
const client = await clientSchema.validate(snapshot.data());

这是一种更好的防御方式,因为如果由于某种原因,错误的数据最终出现在Firestore中,clientSchema.validate将抛出一个错误。在validate()之后,您可以保证clientClient,而不仅仅是告诉TypeScript编译器“将其视为Client,即使在运行时,它可能不是”。

相关问题