小编920*_*sms的帖子

“for...in”语句的左侧不能使用类型注释

我该如何修复该错误The left-hand side of a 'for...in' statement cannot use a type annotation.?未编译的相关代码是一个简单的 for...in 循环:

const roles = ['a', 'b', 'c'] as const
type Roles = typeof roles[number] // 'a' | 'b' | 'c'

const roles = {
  'a': '...',
  'b': '...',
  'c': '...',
}

for (const role: Roles in roles) {
  const isRole = roles.includes(roles)
}
Run Code Online (Sandbox Code Playgroud)

我知道不可能输入常量role,但如果我不这样做,则const isRole = roles.includes(roles)不会有效:Argument of type 'string' is not assignable to parameter of type '"a" | "b" …

types typescript

30
推荐指数
3
解决办法
2万
查看次数

标签 统计

types ×1

typescript ×1