Reputation: 278
I need to convert the following code into 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.
Upvotes: 1
Views: 119
Reputation: 28128
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:
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
};
Upvotes: 4