#들어가며
Compound Component 패턴으로 StickyScroll 컴포넌트를 만들었다. IDE에서 이상한 에러가 뜬다.
Property 'className' is missing in type '{ children: (activeIndex: any) => Element; }'
but required in type '{ [x: string]: any; children: any; className: any; }'.
className은 분명 optional인데 required라고 한다. 더 이상한 건 pnpm run build를 하면 에러가 사라진다. 그런데 CI 환경에서 다시 나타난다.
#문제의 코드
// 서브컴포넌트 정의 - className은 optional
export interface ContentProps extends Omit<
HTMLAttributes<HTMLDivElement>,
"children"
> {
children: ReactNode | ((activeIndex: number) => ReactNode);
}
// HTMLAttributes에서 className은 optional!
function StickyScrollRoot({ children }: StickyScrollProps) {
// ...
}
// ❌ 문제: Object.assign으로 compound component 생성
export const StickyScroll = Object.assign(StickyScrollRoot, {
Container,
Section,
Content,
Indicator,
useStickyScroll,
});
사용할 때:
<StickyScroll.Content>
{(activeIndex) => <div>...</div>} {/* ❌ className이 required라고? */}
</StickyScroll.Content>
#원인: Object.assign의 타입 추론 한계
다이어그램을 불러오는 중입니다.Object.assign 타입 추론 문제 · 원본 다운로드
TypeScript가 Object.assign으로 생성된 객체의 타입을 추론할 때, 각 프로퍼티의 정확한 타입을 유지하지 못하는 경우가 있다.
1. Object.assign으로 compound component 생성
↓
2. TypeScript가 각 서브컴포넌트의 props 타입을 추론
↓
3. 추론 과정에서 타입 정보 손실
↓
4. className이 optional에서 required로 변경됨
#빌드 후 에러가 사라지는 이유
pnpm run build 실행
↓
tsup이 .d.ts 파일 생성 (정확한 타입 정의)
↓
IDE가 새로운 타입 정의 파일 참조
↓
에러 사라짐
빌드 도구가 .d.ts 파일을 생성하면 명시적인 타입이 정의된다. IDE가 이걸 참조하면 문제가 해결된 것처럼 보인다.
#CI에서 다시 에러가 발생하는 이유
CI 환경 (fresh clone)
↓
dist/ 폴더 없음
↓
TypeScript가 소스 코드에서 직접 타입 추론
↓
Object.assign 추론 문제 재발생
CI는 매번 새로 클론하니까 빌드 아티팩트가 없다. 소스에서 직접 추론하면 문제가 다시 나타난다.
#해결책: 명시적 타입 인터페이스
다이어그램을 불러오는 중입니다.명시적 타입으로 해결 · 원본 다운로드
Object.assign의 반환 타입을 명시적으로 선언하면 된다.
import { useState, useCallback, type ReactNode, type FC } from "react";
import { Container, type ContainerProps } from "./views/Container";
import { Section, type SectionProps } from "./views/Section";
import { Content, type ContentProps } from "./views/Content";
import { Indicator, type IndicatorProps } from "./views/Indicator";
// ✅ 명시적인 compound component 타입 정의
interface StickyScrollComponent extends FC<StickyScrollProps> {
Container: FC<ContainerProps>;
Section: FC<SectionProps>;
Content: FC<ContentProps>;
Indicator: FC<IndicatorProps>;
useStickyScroll: typeof useStickyScroll;
}
function StickyScrollRoot({ children }: StickyScrollProps) {
// ...
}
// ✅ 타입 어노테이션으로 정확한 타입 보장
export const StickyScroll: StickyScrollComponent = Object.assign(
StickyScrollRoot,
{
Container,
Section,
Content,
Indicator,
useStickyScroll,
},
);
핵심은 : StickyScrollComponent다. TypeScript의 타입 추론에 의존하지 않고, 직접 타입을 명시한다.
#생성된 .d.ts 비교
#Before (문제 발생)
TypeScript가 잘못 추론한 타입:
export const StickyScroll: (({ children }) => ReactElement) & {
Container: ({
children,
className,
...props
}: {
[x: string]: any;
children: any;
className: any; // ❌ required로 추론됨
}) => ReactElement;
// ...
};
#After (수정 후)
명시적 타입:
interface StickyScrollComponent extends FC<StickyScrollProps> {
Container: FC<ContainerProps>; // ✅ ContainerProps의 className은 optional
Section: FC<SectionProps>;
Content: FC<ContentProps>;
Indicator: FC<IndicatorProps>;
useStickyScroll: typeof useStickyScroll;
}
declare const StickyScroll: StickyScrollComponent;
#Compound Component 타입 정의 템플릿
// 1. 서브컴포넌트 Props 정의
interface SubComponent1Props {
/* ... */
}
interface SubComponent2Props {
/* ... */
}
// 2. Compound Component 타입 정의
interface MyCompoundComponent extends FC<RootProps> {
SubComponent1: FC<SubComponent1Props>;
SubComponent2: FC<SubComponent2Props>;
}
// 3. Object.assign에 타입 어노테이션 적용
export const MyComponent: MyCompoundComponent = Object.assign(RootComponent, {
SubComponent1,
SubComponent2,
});
이 패턴을 따르면 모든 환경에서 일관된 타입을 보장할 수 있다.
#디버깅 팁
#IDE에서 추론된 타입 확인
const StickyScroll = Object.assign(...);
// ^^^^^^^^^^^^^ 여기에 마우스 호버
호버하면 추론된 타입이 표시된다. { [x: string]: any; className: any; } 같은 이상한 타입이 보이면 문제다.
#TypeScript 서버 재시작
타입이 꼬인 것 같으면:
Cmd/Ctrl + Shift + P → "TypeScript: Restart TS Server"
#캐시 정리
rm -rf node_modules/.cache
rm -rf .next/types
#정리
Object.assign으로 Compound Component를 만들면 TypeScript가 서브컴포넌트의 props 타입을 잘못 추론할 수 있다. 빌드 후 에러가 사라지는 건 .d.ts 파일 덕분이고, CI에서 다시 나타나는 건 빌드 아티팩트가 없기 때문이다.
해결책은 명시적인 타입 인터페이스를 선언하는 것이다. Object.assign의 반환 타입을 직접 명시하면 모든 환경에서 일관된 타입을 보장할 수 있다.