我使用TypeScript创建了以下全局变量,并为其指定了适当的数据类型:
reviewkey: string;
title: string;
reviewer: string;
category: string;
header: string;
desc: string;
body: string;
largeimagelink: string;
smallimagelink: string;
feature: string;
previousdate: Date;
spoilers: string;
经过重新考虑后,我想将这些变量创建到单个数组或对象中,这将使我可以通过使用循环将代码显着地简化为更易读的内容。
但是,这样做
reviewProps = [
reviewkey: string;
title: string;
reviewer: string;
category: string;
header: string;
desc: string;
body: string;
largeimagelink: string;
smallimagelink: string;
feature: string;
previousdate: Date;
spoilers: string;
]
导致错误。
什么是最好的方法?
答案 0 :(得分:2)
放入一个单个数组或对象
使用一个对象(不是数组)。示例:
type reviewProps = {
reviewkey: string;
title: string;
reviewer: string;
category: string;
header: string;
desc: string;
body: string;
largeimagelink: string;
smallimagelink: string;
feature: string;
previousdate: Date;
spoilers: string;
}