相关疑难解决方法(0)

(NextAuth) 类型错误:类型“{}”上不存在属性“会话”

我在 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)

typescript reactjs next.js next-auth

25
推荐指数
2
解决办法
2万
查看次数

NextJS _app.tsx Component 和 pageProps 应该是什么 TypeScript 类型?

这是 NextJS 的默认 _app.tsx:

function MyApp({ Component, pageProps }) {
  return (
    <Component {...pageProps} />
  )
}
Run Code Online (Sandbox Code Playgroud)

问题是,一旦你切换到 TypeScript,你就会在 ES6Lint 下收到一个警告,这些类型本质上被设置为类型 'any'。话虽如此,我无法弄清楚将这两个设置为哪种类型,以免以后不匹配的类型导致更多错误。我应该将这两个类型转换为哪些 TypeScript 类型?

javascript typescript reactjs next.js

20
推荐指数
3
解决办法
5636
查看次数

标签 统计

next.js ×2

reactjs ×2

typescript ×2

javascript ×1

next-auth ×1