typescript 팁 - 서로소 유니온 만들기
/** components */
export * from './modules/Artwork';
export { default as Artwork } from './modules/Artwork';
export * from './modules/ButtonBox';
export { default as ButtonBox } from './modules/ButtonBox';
export * from './modules/CardEventInfoList';
export { default as CardEventInfoList } from './modules/CardEventInfoList';
/** union.ts */
import * as components from 'components';
type FirstArgument<T> = T extends (arg1: infer U, ...args: any[]) => any ? U : any;
type ModuleName = keyof typeof components;
type ModuleRecord = {
[name in ModuleName]: {
id?: string;
name: name;
props: FirstArgument<typeof components[name]>;
};
};
type DisjointUnionType = ModuleRecord[keyof ModuleRecord];
요러면 아래같은 타입을 쉽게 만들 수 있다.
type ModuleGeneratorProps = {
id?: string;
name: "Artwork";
props: components.ArtworkProps;
} | {
id?: string;
name: "ButtonBox";
props: components.ButtonBoxProps;
} | {
id?: string;
name: "CardEventInfoList";
props: components.CardEventInfoListProps;
} ... more
좀 더 간단한 방법은 나중에 고민하면 좋을 듯
Author And Source
이 문제에 관하여(typescript 팁 - 서로소 유니온 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@gtah2yk/typescript-팁-서로소-유니온-만들기저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)