-
Hello, I've done a search for similar discussion, but haven't found the exact same problem so I have this one component: -| src
--| components
---| timeline
---| index.ts
---| TimelineList.tsx (this file will be dynamically imported later on) TimelineList component: import { twclsx } from '@/libs/twclsx'
import { Timeline } from './Timeline'
import type { Timeline as TimelineType } from 'rizkicitra'
interface TimelineListProps {
timeline: Array<TimelineType>
}
export const TimelineList: React.FunctionComponent<TimelineListProps> = ({ timeline }) => (
<ul className={twclsx('pl-2')}>
{timeline
.sort((a, b) => (new Date(a.start_date) < new Date(b.start_date) ? 1 : -1))
.map((data: TimelineType, idx: number) => (
<Timeline {...data} key={data.title + idx} />
))}
</ul>
) The
and then I import the component on // pages/about.tsx
const TimelineList = dynamic(() => import('@/components/timeline').then((m) => m.TimelineList), {
loading: () => <Spinner containerSize='full' spinnerSize='md' containerStyle='h-56' />
})
so everything goes well on dev environment, but when I build it for production, there is appear an error like this: Failed to compile.
--
17:10:20.822 |
17:10:20.822 | ./src/pages/about.tsx:12:30
17:10:20.822 | Type error: Argument of type '() => Promise<FunctionComponent<TimelineListProps> \| ComponentClass<never, any> \| FunctionComponent<never> \| { ...; }>' is not assignable to parameter of type 'DynamicOptions<TimelineListProps> \| Loader<TimelineListProps>'. How can I overcome this? and what is the problem here? thank you 🙏🏻 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Hi, AFAIK, the problem is that the development mode, doesn't crash on TypeScript errors. It is the build step that really wants you to fix them in order to complete. So there's nothing weird going on here, other than that there's an actual TypeScript error in your application at all times.
SolutionNow onto the TypeScript error. I've seen this in dynamic imports that do not use the default export. It might have to do with actual React Types and the const TimelineList = dynamic(() => import('@/components/timeline'), {
loading: () => <Spinner containerSize='full' spinnerSize='md' containerStyle='h-56' />
}); Then the error goes away.
Because this type sleight of hand works: const TimelineList = dynamic(() =>
import('@/components/timeline').then<
typeof import('@/components/timeline').TimelineList
>((mod) => mod.TimelineList),
{
loading: () => <Spinner containerSize='full' spinnerSize='md' containerStyle='h-56' />
}
); If you want to avoid the TypeScript sleight of hand, this should do the trick, I think: const TimelineList = dynamic(async () => {
const mod = await import('@/components/timeline');
return mod.TimelineList;
},
{
loading: () => <Spinner containerSize='full' spinnerSize='md' containerStyle='h-56' />
}
); |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
Hi,
AFAIK, the problem is that the development mode, doesn't crash on TypeScript errors. It is the build step that really wants you to fix them in order to complete. So there's nothing weird going on here, other than that there's an actual TypeScript error in your application at all times.
Solution
Now onto the TypeScript error. I've seen this in dynamic imports that do not use the default export. It might have to do with actual React Types and the
dynamic
type fun…