如何在TypeScrypt中使用C#中的元组列表

如何在TypeScrypt中使用C#中的元组列表,c#,json,typescript,C#,Json,Typescript,我想从服务器获取元组列表,并在客户端的TypeScript中使用它们 问题是我不知道如何在TypeScript中声明元组列表? 客户端上的消息属性为空,即使不应为空 这是服务器上实体中的元组列表 public IEnumerable<Tuple<string, string>> Messages { get; set; } 我尝试在TypeScript中创建相同的属性,如下所示: messages: { item1: string, item2: string }[];

我想从服务器获取元组列表,并在客户端的TypeScript中使用它们

问题是我不知道如何在TypeScript中声明元组列表? 客户端上的消息属性为空,即使不应为空

这是服务器上实体中的元组列表

public IEnumerable<Tuple<string, string>> Messages { get; set; }
我尝试在TypeScript中创建相同的属性,如下所示:

messages: { item1: string, item2: string }[];

所有其他属性都可以,问题只在于消息。

您的类型应该可以工作。对于示例数据,这具有预期的输出:

interface Item {
    id: number;
    messages: {
        item1: string;
        item2: string;
    }[];
}

let typed: Item[] = json;
for (const item of typed) {
    console.log(item.id);
    if(!item.messages) continue;
    for(const message of item.messages){
        console.log(`${message.item1} - ${message.item2}`)
    }   
}

你的类型应该有用。对于示例数据,这具有预期的输出:

interface Item {
    id: number;
    messages: {
        item1: string;
        item2: string;
    }[];
}

let typed: Item[] = json;
for (const item of typed) {
    console.log(item.id);
    if(!item.messages) continue;
    for(const message of item.messages){
        console.log(`${message.item1} - ${message.item2}`)
    }   
}

您可以发布JSON的完整类型吗?不仅仅是
消息
。特别是什么是不好的。我不应该work@Paritosh这是您始终可以做的事情,但您应该始终避免您可以发布JSON的完整类型吗?不仅仅是
消息
。特别是什么是不好的。我不应该work@Paritosh这是你永远可以做的事,但你应该避免。谢谢你的帮助。谢谢你的帮助。