0
我正在使用typescript写入类型定义文件(.d.ts)。当我想指定“这个对象内的每个属性是一个字符串”,这样的事情:如何表示此对象内的每个属性都是字符串
interface IThings {
thing: string{}
}
然而,这是行不通的。有什么办法可以做到这一点?
我正在使用typescript写入类型定义文件(.d.ts)。当我想指定“这个对象内的每个属性是一个字符串”,这样的事情:如何表示此对象内的每个属性都是字符串
interface IThings {
thing: string{}
}
然而,这是行不通的。有什么办法可以做到这一点?
您应该使用Indexable Types:
interface IThings {
[name: string]: string;
}
然后:
let a = {} as IThings;
a["x1"] = "y"; // ok
a["x2"] = 4; // Type 'number' is not assignable to type 'string'