Загрузка…
Загрузка…
// Build the four canonical utilities from scratch — this is a common interview ask
type MyPartial<T> = { [K in keyof T]?: T[K] };
type MyReadonly<T> = { readonly [K in keyof T]: T[K] };
type MyPick<T, K extends keyof T> = { [P in K]: T[P] };
type MyRecord<K extends keyof any, V> = { [P in K]: V };
// Key remapping: generate typed event handlers from a state shape
type State = { name: string; age: number };
type Handlers<T> = {
[K in keyof T & string as `on${Capitalize<K>}Change`]: (value: T[K]) => void;
};
type H = Handlers<State>;
// { onNameChange: (v: string) => void; onAgeChange: (v: number) => void }
// Filter by mapping unwanted keys to never
type NonFunctionKeys<T> = {
[K in keyof T]: T[K] extends Function ? never : K;
}[keyof T]; // index by keyof to collapse the object into the surviving union// Homomorphic preservation vs loss
type A = { readonly id: string; name?: string };
type KeepsModifiers = { [K in keyof A]: A[K] }; // readonly + ? preserved ✅
type LosesModifiers = { [K in 'id' | 'name']: string }; // flat, no modifiers ❌Умение собрать Pick/Partial и on${Capitalize<K>}Change handlers — практический минимум по mapped types.
