Skip to content

[문의] p.247 페이지 문의드립니다! #36

@GBAJS754

Description

@GBAJS754

전자책 초판 발행일 : 전자책 2024년 1월15일
페이지 : 247p
내용 :
고차 함수를 활용한 리액트 고차 컴포넌트 만들어보기 예시에서 타입스크립트 오류가 나네요!

interface LoginProps {
  loginRequired?: boolean;
}

function withLoginComponent<T>(Component: ComponentType<T>) {
  return function (props: T & LoginProps) {
    const { loginRequired, ...restProps } = props;

    if (loginRequired) {
      return <>로그인이 필요합니다.</>;
    }

    return <Component {...(restProps as T)} />; // 'T' 형식은 'IntrinsicAttributes & T' 형식에 할당할 수 없습니다.
  };
}

const Component = withLoginComponent((props: { value: string }) => {
  return <h3>{props.value}</h3>;
});

withLoginComponent<T> 부분을 withLoginComponent<T extends { value: string; }>으로 수정하면 해당 오류가 사라지는데 더 좋은 방법이 있다면 알려주시면 감사하겠습니다ㅎㅎ

항상 예시까지 잘 보고있습니다! 감사합니다!

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions