Unverified Commit aca3e989 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Improve error message for wrong props (#41668)

This PR improves the type checking error message when you have unnecessary props:

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

![CleanShot 2022-10-22 at 17 14 38@2x](https://user-images.githubusercontent.com/3676859/197367064-3aac325e-8756-4295-898c-adc52453d088.png)

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent b0f87fbc
...@@ -25,13 +25,20 @@ type TEntry = typeof entry ...@@ -25,13 +25,20 @@ type TEntry = typeof entry
check<IEntry, TEntry>(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 { interface IEntry {
${ ${
options.type === 'layout' options.type === 'layout'
? `default: (props: { children: React.ReactNode; params?: any }) => React.ReactNode | null` ? `default: LayoutComponent`
: `default: (props: { params?: any }) => React.ReactNode | null` : `default: PageComponent`
} }
config?: {} config?: {}
Head?: any
generateStaticParams?: (params?:any) => Promise<any[]> generateStaticParams?: (params?:any) => Promise<any[]>
revalidate?: RevalidateRange<TEntry> | false revalidate?: RevalidateRange<TEntry> | false
dynamic?: 'auto' | 'force-dynamic' | 'error' | 'force-static' dynamic?: 'auto' | 'force-dynamic' | 'error' | 'force-static'
......
...@@ -61,12 +61,19 @@ function getFormattedLayoutAndPageDiagnosticMessageText( ...@@ -61,12 +61,19 @@ function getFormattedLayoutAndPageDiagnosticMessageText(
) )
if (types) { if (types) {
main += '\n' + ' '.repeat(indent * 2) main += '\n' + ' '.repeat(indent * 2)
main += `Expected "${chalk.bold(
types[2].replace( if (types[2] === 'PageComponent') {
'"__invalid_negative_number__"', main += `The exported page component isn't correctly typed.`
'number (>= 0)' } else if (types[2] === 'LayoutComponent') {
) main += `The exported layout component isn't correctly typed.`
)}", got "${chalk.bold(types[1])}".` } else {
main += `Expected "${chalk.bold(
types[2].replace(
'"__invalid_negative_number__"',
'number (>= 0)'
)
)}", got "${chalk.bold(types[1])}".`
}
} }
break break
case 2326: case 2326:
...@@ -80,6 +87,17 @@ function getFormattedLayoutAndPageDiagnosticMessageText( ...@@ -80,6 +87,17 @@ function getFormattedLayoutAndPageDiagnosticMessageText(
main += `Type "${chalk.bold(invalid[1])}" isn't allowed.` main += `Type "${chalk.bold(invalid[1])}" isn't allowed.`
} }
break 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: default:
} }
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
import { useCallback, useState } from 'react' import { useCallback, useState } from 'react'
export default function Page() { export default function Page({ whatIsThis }) {
return <div>hello</div> return <div>hello</div>
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment