将 属性 添加到 Array 打字稿的类型

Add a property to the type of an Array typescript

在开发聊天应用程序时,我在尝试将 属性 添加到对象数组时遇到问题。

    // original array 
    const authors: User[]  = []
    //But I wanted to add something like this
    const authors: User[] & { otherProperty: string }[]  = []

我想我可以将数组扩展到外面然后使它成为一个数组,但是可以用一个交集来完成吗?我真的很好奇

您想将 属性 添加到 User 对吗?

const authors: (User & { otherProperty: string })[] = [];