打字稿:使用特殊字符访问接口键的嵌套对象值

时间:2019-07-16 23:23:34

标签: javascript typescript

我有一个AuthProfile对象,该对象返回动态范围的键:

interface appMetadataProps  {
  signupContext: string
}

interface AuthProfileProps {
 [key: string]: string | boolean | appMetadataProps 

}
const authProfile: AuthProfileProps = Auth.getProfile();

我知道authProfile将包含一个作为字符串url的密钥。我也知道url键将有一个对象作为其值,其中将包含键'signupContext'

let context =
      authProfile['http://myapp.com/app_metadata'].signupContext

^^但是,尝试访问.signupContext值时出现错误。我最初的想法是,由于AuthProfileProps接口中的动态键可以具有appMetaDataProps类型,所以打字稿应该不会给我任何访问signupContext键值的问题。

我在这里做错了什么?

我收到以下错误消息:

Property 'signupContext' does not exist on type 'string | boolean | appMetadataProps'.
  Property 'signupContext' does not exist on type 'string'.ts(2339)

---其他选项----

我的另一个选择是在AuthProfileProps接口中显式定义“ http://myapp.com/app_metadata”作为键。但是由于键名中的冒号(:),打字稿无法将其识别为键。如何用特殊字符指定键名?

interface AuthProfileProps {
 [key: string]: string | boolean ,
 http://myapp.com/app_metadata : appMetaDataProps

}

0 个答案:

没有答案
相关问题