索引签名参数类型不能为联合类型.考虑改用映射对象类型 [英] An index signature parameter type cannot be a union type. Consider using a mapped object type instead
本文介绍了索引签名参数类型不能为联合类型.考虑改用映射对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下模式:
I'm trying to use the following pattern:
enum Option {
ONE = 'one',
TWO = 'two',
THREE = 'three'
}
interface OptionRequirement {
someBool: boolean;
someString: string;
}
interface OptionRequirements {
[key: Option]: OptionRequirement;
}
这对我来说似乎很简单,但是出现以下错误:
This seems very straightforward to me, however I get the following error:
索引签名参数类型不能为联合类型.考虑改用映射的对象类型.
An index signature parameter type cannot be a union type. Consider using a mapped object type instead.
我在做什么错了?
推荐答案
您可以在运算符,然后执行以下操作:
You can use TS "in" operator and do this:
enum Options {
ONE = 'one',
TWO = 'two',
THREE = 'three',
}
interface OptionRequirement {
someBool: boolean;
someString: string;
}
type OptionRequirements = {
[key in Options]: OptionRequirement; // Note that "key in".
}
这篇关于索引签名参数类型不能为联合类型.考虑改用映射对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文