如何使用界面定义文档的结构

问题描述

我有一个描述给定集合中文档结构的接口:

interface IGameDoc {
    playerTurn: string;
    gameState: {
        rowOne: [string,string,string]
        rowTwo: [string,string]
        rowThree: [string,string]
    };
}

因此,如果我获取收藏中的文档

const gamesRef = useFirestore()
    .collection('Games')

const { status,data } = useFirestoreCollectionData(gamesRef) //I would like data to be of type IGameDoc[]

这能以某种方式实现吗?

解决方法

您可以将数据定义为您想要的任何类型,如下所示,但是您无法控制从 useFirestoreCollectionData 服务返回的类型(在这段代码中)。

您要么需要使用函数对其进行转换。

下面的例子将它转换为一个类型,它会尽可能地匹配。如果不了解您的更多代码,我很难进行测试。

const { status,data } : { status: any,data: IGameDoc[]} = useFirestoreCollectionData(gamesRef)