如何用接口定义单据的结构



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

interface IGameDoc {
playerTurn: string;
gameState: {
rowOne: [string, string, string]
rowTwo: [string, string, string]
rowThree: [string, 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)

最新更新