2017-08-01 47 views
1

在我的打字稿的应用程序加载JSON文件后,我使用的接口,用于代码完成在IDE中的JSON数据:如何在JScript数据的Typescript声明文件中使用破折号?

interface Component { 
    name:string 
} 

这样的作品,但JSON还包含有一个破折号属性en-US。 ..这是不允许在一个接口...我怎么能解决这个问题?

{ 
    "name" : "boink", 
    "en-US" : "hello there" 
} 

回答

1

您可以在报价属性的界面就像里面:

interface Component { 
    'en-US': string; 
} 

但是你应该知道,你有你想用它每次都选择通过行情的性质:

let myComponent: Component = { 
    'en-US': 'hello there' 
} 
let translation = myComponent['en-US']; 
+0

谢谢,这是有效的。有没有办法让这个属性成为可选的,就像'锁定? :true',其中'locked'属性是可选的。如果我使用?与''美国'? :string'它不起作用。 – Kokodoko

+1

对我来说''en-US'?'很好(使用打字稿2.4,2) – cyrix