Skip to content

Qvil Blog

[Typescript] Type from object key

Typescript1 min read

Object의 Key를 Type으로 사용하려면 keyof typeof Object

1const MyObject = {
2 key: "key",
3 name: "name",
4};
5
6interface ChildProps {
7 type: keyof typeof MyObject;
8}
9
10const Child = ({ type }: ChildProps) => {
11 return <div>type: {type}</div>;
12};
13
14const Parent = () => {
15 return <Child type="name" />;
16};

type-from-object-key-result.jpg