type User = { age: number name: string sex: 'M' | 'F' }; const UserEntity: User = { age: 1, name: 'name', sex: 'M' } type UserType = typeof UserEntity[keyof typeof UserEntity]; const setValue = (key: UserType) => { switch (key) { case 'number': { console.log(key + 2) } } }
Дай бог тебе здоровья дотрый человек
type User = { age: number; name: string; sex: "M" | "F"; }; const setValue = <T extends User, K extends keyof User>(key: K) => (value: T[K]) => { switch (key) { case "age": { console.log(Number(value) + 2); // expected number } case "name": { console.log(value + "dsadsadsa"); // expected string } case "sex": { console.log(value); // expected 'M' | 'F' } } };
Обсуждают сегодня