프록시 컴포넌트 작성하기
Q. 표준 HTML 컴포넌트를 많이 사용하는데, 이때 필요한 모든 프로퍼티를 항상 설정하는 작업이 번거로움 A. 프록시 컴포넌트를 만들고 적용한다.
ㅅㄷㄴㅅ
특정 속성 재정의 못하게 하기
type SubmitButtonProps = Omit<JSX.IntrinsicElements["button"], "type">;
function SubmitButton(props: SubmitButtonProps) {
return <button type="submit" {...props} />;
}
type StyleButton = Omit<
JSX.IntrinsicElements["button"],
"type" | "className" | "style"
> & { type: "primary" | "secondary" };
function StyledButton({ type, ...allProps }: StyledButton) {
return <Button type="button" className={`btn-${type}`} {...allProps} />;
}
특정 속성만 필수 값으로 정의하기
type MakeRequired<T, K extends keyof T> = Omit<T, K> & Required<Pick<T, K>>;
type ImgProps = MakeRequired<JSX.IntrinsicElements["img"], "alt" | "src">;
export function Img(prps: ImgProps) {
return <img {...props} />;
}
const anImage = <Img />;
// alt, src 속성만 필수여서 ts 에러가 남