1.10.12.2. fejezet, Index signature
Beküldte pzoli - 2023, augusztus 15 - 3:17du
interface TransactionObj { [index: string]: number, //Index signature Pizza: number, Books: number, Job: number } const todayTransactions: TransactionObj = { Pizza: 5, Books: 10, Job: 100 } const prop: string = "Pizza"; console.log(todayTransactions[prop]); const todayNet = (transactions: TransactionObj): number => { let total = 0; for (let filed in transactions) { total += transactions[filed]; } return total; } console.log(todayNet(todayTransactions));
interface Student { name: string, age: number, classes: number[] } const student: Student = { name: 'Amy', age: 19, classes: [1, 2, 3] } for (const key in student) { console.log(`${key}=${student[key as keyof Student]}`) } // vagy for (const key in student) { console.log(`${key}=${student[key as keyof typeof student]}`) } // vagy function logStudentKey(student: Student, key: keyof Student): void { console.log(`${key}=${student[key]}`) } Object.keys(student).map((key) => { logStudentKey(student, key as keyof Student) })
- A hozzászóláshoz be kell jelentkezni