Proste ćwiczenie z mapped types i capitalize. Kontynuacja lekcji poprzednich. Do dzieła.
Ok, przypomnijmy sobie ten interfejs:
interface Employee2 {
id: number;
name: string;
age: number;
salary: number;
}
A teraz te typy, ja już nie podpowiadam co one robią:
type CapitalizeKeys<T> = {
[P in keyof T as `${Capitalize<string & P>}`]: T[P];
};
type Getter<T> = {
[P in keyof T as `get${Capitalize<string & P>}`]: () => T[P]
};
type Setter<T> = {
[P in keyof T as `set${Capitalize<string & P>}`]: (newVal: T[P]) => boolean
};
type WithGetters<T> = {
[P in keyof T]: T[P];
} & {
[P in keyof T as `get${Capitalize<string & P>}`]: () => T[P]
};
type WithGetterSetters<T> = {
[P in keyof T]: T[P];
} & {
[P in keyof T as `get${Capitalize<string & P>}`]: () => T[P]
} & {
[P in keyof T as `set${Capitalize<string & P>}`]: (newVal: T[P]) => boolean
};
Ok, to teraz taki typ:
type Listeners<T> = {
[P in keyof T as `on${Capitalize<string & P>}Change`]: (newValue: T[P]) => void;
}
type EmpListeners = Listeners<Employee2>
Mam nadzieję, że dobrze zgadliśmy:
type Listeners<T> = {
[P in keyof T as `on${Capitalize<string & P>}Change`]: (newValue: T[P]) => void;
}
type EmpListeners = Listeners<Employee2>
// type EmpListeners = {
// onIdChange: (newValue: number) => void;
// onNameChange: (newValue: string) => void;
// onAgeChange: (newValue: number) => void;
// onSalaryChange: (newValue: number) => void;
// }
Więcej TSa niedługo!