avatar
define interface for an array of objects TypeScript

> Need Interface:

declare interface StudentInfo {
    id: number;
    name: string;
    mssv: any;
}

/* For nested */
declare interface StudentItems {
  Items: StudentInfo[],
}

> Define Global Variable:

export default class StudentInfoForm extends Component {

    private studentItems: StudentInfo[];
}

> How to Apply:

 constructor() {
    this.studentItems = [];
 }

 data.forEach((element: ProfileInfo) => {
  if (element.is_update === 'Y') {
    const temp = {} as StudentInfo;
    temp.id = element.id;
    temp.name = element.name;
    temp.mssv = element.mssv;
    this.studentItems.push(temp);
  }
 
  /** Another case */
  const student: StudentInfo = {
      id: 'Luzon',
      name: 3,
      mssv: 'DFDS343DF'
  };
});
You need to login to do this manipulation!