typescript keyof

example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
export type User = {
name: string;
email: string;
};

const user: User = {
name: 'chinsung',
email: 'chin_sung@naver.com',
};

// keyof User === 'name' | 'email'
const updateUser = (name: keyof User, value: string) => {
return {
...user,
[name]: value,
};
};

updateUser(''); // wow 자동완성!!
  • 나이수

참고