[Typescript] 특정 컴포넌트의 props 타입을 사용하고 싶다면?

Bewell·2023년 12월 6일
0

React에서 특정 컴포넌트에서 쓰이는 props들의 타입을 별도의 타입으로 선언해서 사용하고 싶다면 'componentProps' 기능을 사용하면 된다.

import { ComponentProps } from 'react'

//	HTML Element
type Props = ComponentProps<'button'>
                            
//	사용자정의 컴포넌트일때는 'typeof'를 추가한다
type Props = ComponentProps<typeof 사용자정의컴포넌트이름>
                            

타입을 확인할 수 있고, 위 값을 상속받아 추가할 수도 있다

type Props = ComponentProps<typeof 사용자정의컴포넌트이름> & {
	name: string
  	age: number
}




참고링크

0개의 댓글