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

Improve type checking (#46276)

parent 5a304817
......@@ -75,7 +75,7 @@ export interface PageProps {
searchParams?: any
}
export interface LayoutProps {
children: React.ReactNode
children?: React.ReactNode
${
options.slots
? options.slots.map((slot) => ` ${slot}: React.ReactNode`).join('\n')
......
......@@ -41,27 +41,26 @@ function getFormattedLinkDiagnosticMessageText(
}
function getFormattedLayoutAndPageDiagnosticMessageText(
baseDir: string,
relativeSourceFilepath: string,
diagnostic: import('typescript').Diagnostic
) {
const message = diagnostic.messageText
const sourceFilepath =
diagnostic.file?.text.trim().match(/^\/\/ File: (.+)\n/)?.[1] || ''
const message =
typeof diagnostic.messageText === 'string'
? diagnostic
: diagnostic.messageText
const messageText = message.messageText
if (sourceFilepath && typeof message !== 'string') {
const relativeSourceFile = path.relative(baseDir, sourceFilepath)
const type = /page\.[^.]+$/.test(relativeSourceFile) ? 'Page' : 'Layout'
if (typeof messageText === 'string') {
const type = /page\.[^.]+$/.test(relativeSourceFilepath) ? 'Page' : 'Layout'
// Reference of error codes:
// https://github.com/Microsoft/TypeScript/blob/main/src/compiler/diagnosticMessages.json
switch (message.code) {
case 2344:
const filepathAndType = message.messageText.match(
/typeof import\("(.+)"\)/
)
const filepathAndType = messageText.match(/typeof import\("(.+)"\)/)
if (filepathAndType) {
let main = `${type} "${chalk.bold(
relativeSourceFile
relativeSourceFilepath
)}" does not match the required types of a Next.js ${type}.`
function processNext(
......@@ -173,16 +172,16 @@ function getFormattedLayoutAndPageDiagnosticMessageText(
}
}
processNext(1, message.next)
if ('next' in message) processNext(1, message.next)
return main
}
const invalidExportFnArg = message.messageText.match(
const invalidExportFnArg = messageText.match(
/Type 'OmitWithTag<(.+), .+, "(.+)">' does not satisfy the constraint/
)
if (invalidExportFnArg) {
const main = `${type} "${chalk.bold(
relativeSourceFile
relativeSourceFilepath
)}" has an invalid "${chalk.bold(
invalidExportFnArg[2]
)}" export:\n Type "${chalk.bold(
......@@ -191,12 +190,12 @@ function getFormattedLayoutAndPageDiagnosticMessageText(
return main
}
const invalidExportFnReturn = message.messageText.match(
const invalidExportFnReturn = messageText.match(
/Type '{ __tag__: "(.+)"; __return_type__: (.+); }' does not satisfy/
)
if (invalidExportFnReturn) {
let main = `${type} "${chalk.bold(
relativeSourceFile
relativeSourceFilepath
)}" has an invalid export:\n "${chalk.bold(
invalidExportFnReturn[2]
)}" is not a valid ${invalidExportFnReturn[1]} return type:`
......@@ -226,29 +225,52 @@ function getFormattedLayoutAndPageDiagnosticMessageText(
}
}
processNext(1, message.next)
if ('next' in message) processNext(1, message.next)
return main
}
break
case 2345:
const filepathAndInvalidExport = message.messageText.match(
const filepathAndInvalidExport = messageText.match(
/'typeof import\("(.+)"\)'.+Impossible<"(.+)">/
)
if (filepathAndInvalidExport) {
const main = `${type} "${chalk.bold(
relativeSourceFile
relativeSourceFilepath
)}" exports an invalid "${chalk.bold(
filepathAndInvalidExport[2]
)}" field. ${type} should only export a default React component and configuration options. Learn more: https://nextjs.org/docs/messages/invalid-segment-export`
return main
}
break
case 2559:
const invalid = messageText.match(
/Type '(.+)' has no properties in common with type '(.+)'/
)
if (invalid) {
const main = `${type} "${chalk.bold(
relativeSourceFilepath
)}" contains an invalid type "${chalk.bold(invalid[1])}" as ${
invalid[2]
}.`
return main
}
break
default:
}
}
}
function getAppEntrySourceFilePath(
baseDir: string,
diagnostic: import('typescript').Diagnostic
) {
const sourceFilepath =
diagnostic.file?.text.trim().match(/^\/\/ File: (.+)\n/)?.[1] || ''
return path.relative(baseDir, sourceFilepath)
}
export function getFormattedDiagnostic(
ts: typeof import('typescript'),
baseDir: string,
......@@ -263,14 +285,18 @@ export function getFormattedDiagnostic(
let message = ''
const appPath = isLayoutOrPageError
? getAppEntrySourceFilePath(baseDir, diagnostic)
: null
const linkReason = getFormattedLinkDiagnosticMessageText(diagnostic)
const layoutReason =
!linkReason && isLayoutOrPageError
? getFormattedLayoutAndPageDiagnosticMessageText(baseDir, diagnostic)
const appReason =
!linkReason && isLayoutOrPageError && appPath
? getFormattedLayoutAndPageDiagnosticMessageText(appPath, diagnostic)
: null
const reason =
linkReason ||
layoutReason ||
appReason ||
ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')
const category = diagnostic.category
switch (category) {
......@@ -325,6 +351,8 @@ export function getFormattedDiagnostic(
},
{ forceColor: true }
)
} else if (isLayoutOrPageError && appPath) {
message = chalk.cyan(appPath) + '\n' + message
}
return message
......
import type { PropsWithChildren } from 'react'
type LayoutProps = PropsWithChildren<{}>
export default function Layout(props: LayoutProps) {
return <div>{props.children}</div>
}
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"incremental": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"plugins": [
{
"name": "next"
}
],
"strictNullChecks": true
},
"include": ["next-env.d.ts", ".next/types/**/*.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules"]
}
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