TS 中的索引签名和键入的密钥?
index signature and typed key in TS?
以下代码总是会产生错误,但作为草图,这就是我要表达的意思:
interface A {
[ key : string ] : string,
"anotherKey" : MyInterface
}
我查了几本书,TS 文档,在 ts-node 中测试过,但我不是很精通,需要一些帮助。
如何指定一个对象,其大多数键是 "key":"value"
但某些特定键不是?
使用常规类型和交集:
type MySpecialType = {
specific: { type: number; };
extremely: { specific: { type: string; } };
} & {
[key: string]: string;
};
&
读作和
所以这是说这是一个非常具体的类型和字符串到字符串的索引签名。
以下代码总是会产生错误,但作为草图,这就是我要表达的意思:
interface A {
[ key : string ] : string,
"anotherKey" : MyInterface
}
我查了几本书,TS 文档,在 ts-node 中测试过,但我不是很精通,需要一些帮助。
如何指定一个对象,其大多数键是 "key":"value"
但某些特定键不是?
使用常规类型和交集:
type MySpecialType = {
specific: { type: number; };
extremely: { specific: { type: string; } };
} & {
[key: string]: string;
};
&
读作和
所以这是说这是一个非常具体的类型和字符串到字符串的索引签名。