Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix typings of dynamic suspense #28740

Merged
merged 3 commits into from
Sep 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions packages/next/shared/lib/dynamic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,18 @@ export type LoadableBaseOptions<P = {}> = LoadableGeneratedOptions & {
ssr?: boolean
}

export type LoadableSuspenseOptions<P = {}> = {
loader: Loader<P>
export type LoadableSuspenseOptions = {
suspense?: boolean
}

export type LoadableOptions<P = {}> = LoadableBaseOptions<P>

export type DynamicOptions<P = {}> = LoadableBaseOptions<P>
export type DynamicOptions<P = {}> =
| LoadableBaseOptions<P>
| LoadableSuspenseOptions

export type LoadableFn<P = {}> = (
opts: LoadableOptions<P> | LoadableSuspenseOptions<P>
opts: LoadableOptions<P> | LoadableSuspenseOptions
) => React.ComponentType<P>

export type LoadableComponent<P = {}> = React.ComponentType<P>
Expand Down Expand Up @@ -113,7 +114,9 @@ export default function dynamic<P = {}>(
// Support for passing options, eg: dynamic(import('../hello-world'), {loading: () => <p>Loading something</p>})
loadableOptions = { ...loadableOptions, ...options }

const suspenseOptions = loadableOptions as LoadableSuspenseOptions<P>
const suspenseOptions = loadableOptions as LoadableSuspenseOptions & {
loader: Loader<P>
}
if (!process.env.__NEXT_CONCURRENT_FEATURES) {
// Error if react root is not enabled and `suspense` option is set to true
if (!process.env.__NEXT_REACT_ROOT && suspenseOptions.suspense) {
Expand Down
3 changes: 3 additions & 0 deletions test/integration/react-18/app/components/ts-foo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function TsFoo() {
return <div>ts-foo</div>
}
14 changes: 14 additions & 0 deletions test/integration/react-18/app/pages/suspense/typing.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React, { Suspense } from 'react'
import dynamic from 'next/dynamic'

const DynamicFoo = dynamic(() => import('../../components/ts-foo'), {
suspense: true,
})

export default function Typing() {
return (
<Suspense fallback={null}>
<DynamicFoo />
</Suspense>
)
}
19 changes: 19 additions & 0 deletions test/integration/react-18/app/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}