我在 NextJs 项目上使用 NextAuth,但收到错误"Type error: Property 'session' does not exist on type '{}'."。session我按照此处的建议将该属性添加到我的 _app.tsx 中:
https://next-auth.js.org/getting-started/example
我还将该属性添加到了我的自定义MyApp类型接口中,但仍然收到错误。按照我的代码:
import { NextComponentType } from "next";
import { Session } from "next-auth";
export interface CustomAppProps extends AppProps {
Component: NextComponentType & { auth?: boolean; session?: Session };
}
function MyApp({ Component, pageProps: { session, ...pageProps } }: CustomAppProps) {
//...
});
Run Code Online (Sandbox Code Playgroud)
我该如何修复它?谢谢!
编辑#1(添加MyApp我当前的代码):
function MyApp({ Component, pageProps: { session, ...pageProps } }: CustomAppProps) {
return …Run Code Online (Sandbox Code Playgroud) 这是 NextJS 的默认 _app.tsx:
function MyApp({ Component, pageProps }) {
return (
<Component {...pageProps} />
)
}
Run Code Online (Sandbox Code Playgroud)
问题是,一旦你切换到 TypeScript,你就会在 ES6Lint 下收到一个警告,这些类型本质上被设置为类型 'any'。话虽如此,我无法弄清楚将这两个设置为哪种类型,以免以后不匹配的类型导致更多错误。我应该将这两个类型转换为哪些 TypeScript 类型?