arrays - Working with types in typescript -
i declare object has following structure
public car = { price: 20000, currency: eur, seller: null, model: { color: null, type: null, year: null } array<object> }; then, when work object, have like
public addproduct(typeid: number): void { this.car.model.push({type: typeid}); } the problem facing when defined model object, using as array<object> generates alone lines
type '{ color: null; type: null; year: null; }' cannot converted type 'object[]'. property 'length' missing in type '{ color: null; type: null; year: null; } i couldn't find proper why define this. important use push generate "empty" object can add attributes view.
you can create object in typescript
let car: = { price: 20000, currency: 'eur', seller: null, model: [ { color: 'red', type: 'one', year: '2000' }, { color: 'blue', type: 'two', year: '2001' } ] } then can wanted
car.model.push({ color: 'green', type: 'three', year: '2002' }); to add new model, or fetch one
car.model[0] // returns { color: 'red', type: 'one', year: '2000' } another alternative create class instead of object
export class car { public price: number; public currency: string; public seller: string; public models: any[]; constructor() { } } and put appropriate methods inside class.
Comments
Post a Comment