【发布时间】:2019-07-16 05:47:52
【问题描述】:
我希望下面的代码可以工作,但不知道为什么不行:
interface Person {
firstName: string
}
const property: 'Name' = 'Name'
const zack: Person = {
[`first${property}`]: 'Zack'
}
创建zack 会引发错误:
Property 'firstName' is missing in type '{ [x: string]: string; }' but required in type 'Person'.
更多地玩这个,任何字符串连接似乎都是问题:
type EqualsFirstName = 'firstName'
const value: EqualsFirstName = 'first' + 'Name'
这也不行。
【问题讨论】:
-
这是我在 TypeScript REPL 中所指的更简单的示例:typescriptlang.org/play/#code/…
-
仅供参考,这是 TypeScript 存储库中关于此功能实现的一个持续问题:github.com/microsoft/TypeScript/issues/…
标签: typescript template-literals