[英]JS Code convert to Typescript
我需要将以下代码转换为TypeScript。 它是带有对象的数组。 然后我想在一个类中使用它。 我尝试使用一个接口,但是不能做太多事情。 请帮我。
var accountModel = {
name: 'ABC Account',
accounts: [
{ balance: 10000, description: 'Checking' },
{ balance: 50000, description: 'Savings' }
]
};
非常感激你的帮助。
如果要对帐户模型数据进行类型检查,则可以使用类型别名
type AccountModel = {name:string, accounts:Array<Account>}
type Account = {balance:number, description:string}
现在,IDE将检查您的变量是否具有正确的内容:
let acc : AccountModel = {
name: 'ABC Account',
accounts: [
{ balance: 10000, description: 'Checking' },
{ balance: 50000, description: 'Savings' }
],
test: 'hello' // The IDE will complain: 'test' is
// not assignable to accountModel
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.