简体   繁体   English

JS代码转换为Typescript

[英]JS Code convert to Typescript

I need to convert the following code into TypeScript. 我需要将以下代码转换为TypeScript。 It is an array with objects. 它是带有对象的数组。 and then I want to use it inside a class. 然后我想在一个类中使用它。 I tried to use an interface but I could not proceed much. 我尝试使用一个接口,但是不能做太多事情。 Please help me. 请帮我。

var accountModel = {
        name: 'ABC Account',
        accounts: [
            { balance: 10000, description: 'Checking' },
            { balance: 50000, description: 'Savings' }
        ]
    };

Your help is very much appreciated. 非常感激你的帮助。

If you want type checking on your account model data, you can use type aliases 如果要对帐户模型数据进行类型检查,则可以使用类型别名

type AccountModel = {name:string, accounts:Array<Account>}
type Account = {balance:number, description:string}

Now your IDE will check if your variable has the correct content: 现在,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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM