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

Improve error message for wrong props #41668

Merged
merged 1 commit into from Oct 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions packages/next/build/webpack/plugins/flight-types-plugin.ts
Expand Up @@ -25,13 +25,20 @@ type TEntry = typeof entry

check<IEntry, TEntry>(entry)

type PageProps = { params?: any }
type LayoutProps = { children: React.ReactNode; params?: any }

type PageComponent = (props: PageProps) => React.ReactNode | null
type LayoutComponent = (props: LayoutProps) => React.ReactNode | null

interface IEntry {
${
options.type === 'layout'
? `default: (props: { children: React.ReactNode; params?: any }) => React.ReactNode | null`
: `default: (props: { params?: any }) => React.ReactNode | null`
? `default: LayoutComponent`
: `default: PageComponent`
}
config?: {}
Head?: any
generateStaticParams?: (params?:any) => Promise<any[]>
revalidate?: RevalidateRange<TEntry> | false
dynamic?: 'auto' | 'force-dynamic' | 'error' | 'force-static'
Expand Down
30 changes: 24 additions & 6 deletions packages/next/lib/typescript/diagnosticFormatter.ts
Expand Up @@ -61,12 +61,19 @@ function getFormattedLayoutAndPageDiagnosticMessageText(
)
if (types) {
main += '\n' + ' '.repeat(indent * 2)
main += `Expected "${chalk.bold(
types[2].replace(
'"__invalid_negative_number__"',
'number (>= 0)'
)
)}", got "${chalk.bold(types[1])}".`

if (types[2] === 'PageComponent') {
main += `The exported page component isn't correctly typed.`
} else if (types[2] === 'LayoutComponent') {
main += `The exported layout component isn't correctly typed.`
} else {
main += `Expected "${chalk.bold(
types[2].replace(
'"__invalid_negative_number__"',
'number (>= 0)'
)
)}", got "${chalk.bold(types[1])}".`
}
}
break
case 2326:
Expand All @@ -80,6 +87,17 @@ function getFormattedLayoutAndPageDiagnosticMessageText(
main += `Type "${chalk.bold(invalid[1])}" isn't allowed.`
}
break
case 2741:
const incompatProp = item.messageText.match(
/Property '(.+)' is missing in type 'PageProps'/
)
if (incompatProp) {
main += '\n' + ' '.repeat(indent * 2)
main += `Prop "${chalk.bold(
incompatProp[1]
)}" will never be passed. Remove it from the component's props.`
}
break
default:
}

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/app-dir/app-typescript/app/inner/page.tsx
Expand Up @@ -4,7 +4,7 @@

import { useCallback, useState } from 'react'

export default function Page() {
export default function Page({ whatIsThis }) {
return <div>hello</div>
}

Expand Down