通过枚举访问对象返回任何而不是未定义的
具有这样的结构:
enum Company {
Google,
Facebook
}
function getRating(company: Company) {
const ratingMap = {
[Company.Google]: 1000,
[Company.Facebook]: 2000
}
const result = ratingMap[company]
return result
}
打字稿理解结果
是号码
。但是,如果我列出并非全部 company
contingMap
中的值,那么result
具有类型。我希望ts将此表达式解析为number |未定义的
而不是任何
。
function getRating(company: Company) {
const ratingMap = {
[Company.Google]: 1000,
// [Company.Facebook]: 2000
}
const result = ratingMap[company] // Element implicitly has an 'any'
return result
}
I know I can set a type for ratingMap
like this:
const ratingMap: Partial<Record<Company, number>>
and it would给我预期的号码|未定义
。但是我认为应该有一个更简单的优雅解决方案。也许是特定的tsconfig属性。
有什么想法如何实现这一目标?
Having a structure like this:
enum Company {
Google,
Facebook
}
function getRating(company: Company) {
const ratingMap = {
[Company.Google]: 1000,
[Company.Facebook]: 2000
}
const result = ratingMap[company]
return result
}
TypeScript understands that result
is a number
. But if I list not all the Company
values in the ratingMap
, then the result
has type any
. I want ts to resolve this expression into number | undefined
rather than any
.
function getRating(company: Company) {
const ratingMap = {
[Company.Google]: 1000,
// [Company.Facebook]: 2000
}
const result = ratingMap[company] // Element implicitly has an 'any'
return result
}
I know I can set a type for ratingMap
like this:
const ratingMap: Partial<Record<Company, number>>
and it would give me expected number | undefined
. But I think there should be a simpler elegant solution. Maybe a specific tsconfig property.
Any ideas how to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过
,应该会出现您想要的东西。
Have you tried
This should do exectly what you want.
使用ES6地图。 Playground here.
Use the es6 map. Playground here.
结果的类型
不是任何,它只是由于错误而被推断为任何。具体而言,错误是您 index 一种缺少索引签名带有0 |的数字结合1
(company.google的值
和company.facebook
)分别推迟推断哪种类型contingmap
是对编译器的。毫不奇怪,该类型被推断为:如果您启用 >向后兼容编译器标志,您会注意到,当用
,因为标志简单地抑制了错误)。Company
索引contingMap
时,该错误消失代码>任何You need to explicitly tell the compiler what
ratingMap
is: a partial record with keys of typeCompany
and values of typenumber
, which is trivially expressed作为partial&lt;记录&lt; company,编号&gt;
。结果
将正确推断为号码| undefind
然后:playground
另一种解决方案是确保编译器
company> company
绝对是使用用户定义的类型守护的评级最>
的关键。这可能是最类型的安全方法:Playground
The type of the
result
is notany
, it is just inferred asany
due to the error. Specifically, the error is that you index a type that lacks an index signature with a numeric union of0 | 1
(values ofCompany.Google
andCompany.Facebook
respectively) while deferring inference of what typeratingMap
is to the compiler. Unsurprisingly, the type is inferred as:If you enable the
suppressImplicitAnyIndexErrors
backwards compatibility compiler flag, you will notice that the error disappears when indexing theratingMap
withCompany
(although theresult
will still be inferred asany
because the flag simply suppresses the error).You need to explicitly tell the compiler what
ratingMap
is: a partial record with keys of typeCompany
and values of typenumber
, which is trivially expressed asPartial<Record<Company, number>>
. Theresult
type will correctly be inferred asnumber | undefind
then:Playground
An alternative solution is to ensure the compiler that
company
is definitely a key on theratingMap
with a user-defined type guard. This is probably the most type-safe way:Playground