Declare an interface for the Icons import by import * as Icons from './icons' statement.
./icons/SquareIcon.tsx:
import React from 'react';
export default function SquareIcon() {
return <div>square icon</div>;
}
./icons/CycleIcon.tsx:
import React from 'react';
export default function CycleIcon() {
return <div>cycle icon</div>;
}
./icons/index.ts:
import CycleIcon from './CycleIcon';
import SquareIcon from './SquareIcon';
export interface IconType {
cycle: typeof CycleIcon;
square: typeof SquareIcon;
}
export { CycleIcon as cycle, SquareIcon as square };
Icon.tsx:
import React from 'react';
import * as Icons from './icons';
import type { IconType } from './icons';
const Icon = ({ icon, ...props }: { icon: keyof IconType }) => {
const Icon = Icons[icon];
return <Icon {...props} />;
};
function Test() {
return <Icon icon="cycle"></Icon>;
}