Environment: Angular 6:
I have a blank array defined in the class
and interface
as below: and
export class Student {
fullName: string;
age: number;
classType: string;
}
export class MyClass {
public resultData: Array<Student> = [];
processedData = {
studentData: [
{
name: 'Nayan',
type: 'toddler',
classType: 'secondary'
},
{
name: 'Rajesh',
type: 'infant',
classType: 'primary'
},
{
name: 'Rithik',
type: 'toddler',
classType: 'secondary'
},
{
name: 'Rob',
type: 'toddler',
classType: 'primary'
},
{
name: 'Daneirl',
type: 'toddler',
classType: 'secondary'
}
]
}
resultData1 =[
{name:'nayan', age:8, classType:'primary'},
{name:'Daniel', age:15, classType:'scondary'},
{name:'Rithik', age:12, classType:'secondary'}
]
someEventClickedFormView() {
this.processedData.studentData.forEach((data, index) => {
switch (data.classType) {
case 'primary': {
this.processName(data, 'fullName', index);
this.processAge(data,'age',index);
break;
}
case 'secondary': {
this.processName(data, 'fullName', index);
this.processAge(data,'age',index);
break;
}
}
})
}
public processName(data: any, fieldName: string, index: number) {
this.resultData.push({ fieldName: data.fullName })
// here I am getting error
// Argument of type '{ fieldName: any; }' is not assignable to parameter of type 'Student'.
// Object literal may only specify known properties, and 'fieldName' does not exist in type 'Student'.ts(2345)
}
public processAge(data:any,fieldName:string,index:number) {
this.resultData.push({ fieldName: this.someTransformationToAge(data.age)})
}
}
at button click event which is here someEventClickedFormView being called and pushes the data into resultData coming from mock data.
However, I am getting errors as below: you can see above code section push method.
Argument of type '{ fieldName: any; }' is not assignable to parameter of type 'Student'.
Object literal may only specify known properties, and 'fieldName' does not exist in type 'Student'.ts(2345)
basically, I want resultData
as below
resultData =[
{name:'nayan', age:8, classType:'primary'},
{name:'Daniel', age:15, classType:'scondary'},
{name:'Rithik', age:12, classType:'secondary'}
]