https://github.com/woowa-typescript/woowahan-typescript-with-react-example-code/tree/main/8장

8.1 리액트 컴포넌트의 타입

1. 클래스 컴포넌트 타입

interface Component <P = {}, S = {}, SS = any> extends ComponentLifecycle<P, S, SS> {}

class Component<P, S> {
	/* ... 생략 */
}

class PureComponent<P = {}, S = {}, SS = any> extends Component <P, S, SS> {}
interface WelcomProps {
	name : string
}

class Welcome extends React.Component<WelcomeProps> {
	/* ... 생략 */
}

2. 함수 컴포넌트 타입

// 함수 선언을 사용한 방식
function Welcome(props: WelcomeProps) : JSX.Element {}

// 함수 표현식을 사용한 방식 - React.FC 사용
const Welcome: React.FC<WelcomeProps> = ({name}) => {};

// 함수 표현식을 사용한 방식 - JSX.Element를 반환 타입으로 지정
const Welcome = ({name}: WelcomeProps): JSX.Element => {};

3. Children props 타입 지정

type PropsWithChildren<P> = P & { children?: ReactNode | undefined };
// example 1
type WelcomeProps = {
  children: "천생연분" | "더 귀한 분" | "귀한 분" | "고마운 분";
};

// example 2
type WelcomeProps = { children: string };

// example 3
type WelcomeProps = { children: ReactElement };

4. render 메서드와 함수 컴포넌트의 반환 타입 - React.ReactElement vs JSX.Element vs React.ReactNode

interface ReactElement<
  P = any,
  T extends string | JSXElementConstructor<any> =
    | string
    | JSXElementConstructor<any>
> {
  type: T;
  props: P;
  key: Key | null;
}