1.10.12.2. fejezet, Index signature

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)
})
function getObjectProp<T, K extends keyof T>(obj: T[], key: K): T[K][] {
    return obj.map(o => o[key])
}
 
const students: Student[] = [
    {
        name: "Aliz",
        age: 18,
        classes: [1, 2]
    },
    {
        name: "Tibi",
        age: 21,
        classes: [1, 2, 4]
    }
]
 
console.log(getObjectProp(students, "name"));