我目前正在处理一个Next.js 13.4项目,并try 使用应用程序/路由设置NextAuth.但是,我遇到了一个似乎无法解决的输入错误.

以下是我的route.ts文件:

import NextAuth, { AuthOptions } from "next-auth";
import DiscordProvider from "next-auth/providers/discord";

export const authOptions: AuthOptions = {
  providers: [
    DiscordProvider({
      clientId: process.env.CLIENT_ID as string,
      clientSecret: process.env.CLIENT_SECRET as string,
    }),
  ],
  session: {
    strategy: "jwt",
  },
  secret: process.env.NEXTAUTH_SECRET,
}

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST }

以下是运行‘npm Run Build’时的错误消息:

- info Linting and checking validity of types ...Failed to compile.

.next/types/app/api/auth/[...nextauth]/route.ts:8:13
Type error: Type 'OmitWithTag<typeof import("C:/Users/Luk/Documents/Workspace/zerotwo-dash/src/app/api/auth/[...nextauth]/route"), "GET" | "POST" | "HEAD" | "OPTIONS" | "PUT" | "DELETE" | "PATCH" | "config" | ... 6 more ... | "runtime", "">' does not satisfy the constraint '{ [x: string]: never; }'.
  Property 'authOptions' is incompatible with index signature.
    Type 'AuthOptions' is not assignable to type 'never'.

   6 |
   7 | // Check that the entry is a valid entry
>  8 | checkFields<Diff<{
     |             ^
   9 |   GET?: Function
  10 |   HEAD?: Function
  11 |   OPTIONS?: Function

我真的不知道这里发生了什么.在Nextauth的GitHub页面上查找‘AuthOptions’时,我发现我的代码没有任何问题.

如果有任何关于如何解决这个问题的见解或建议,我将不胜感激.提前谢谢!

推荐答案

好的,我自己解的.对于任何有相同问题的人,我创建了一个新文件@/utils/authOptions.ts:

import { NextAuthOptions } from "next-auth";
import DiscordProvider from "next-auth/providers/discord";

export const authOptions: NextAuthOptions = {
    providers: [
        DiscordProvider({
            clientId: process.env.CLIENT_ID as string,
            clientSecret: process.env.CLIENT_SECRET as string,
        }),
    ],
    session: {
        strategy: "jwt",
    },
    secret: process.env.NEXTAUTH_SECRET,
}

并在@/api/auth/[...nextauth]/route.ts中使用:

import { authOptions } from "@/utils/authOptions";
import NextAuth from "next-auth/next";

const handler = NextAuth(authOptions);

export { handler as GET, handler as POST };

我稍微更改了导入(从"Next-auth/Next"导入NextAuth) 我不知道为什么这个管用,但它确实管用.即使像以前一样在我的route.ts中改变进口也不能解决它.如果它像这样分开的话……

Typescript相关问答推荐

为什么当类类型与方法参数类型不兼容时,该函数可以接受类类型

排序更改数组类型

类型脚本不会推断键的值类型

创建一个通用上下文,允许正确推断其中的子项

为什么缩小封闭内部不适用于属性对象,但适用于声明的变量?

Angular 17 -如何使用新的@if语法在对象中使用Deliverc值

TypeScript将键传递给新对象错误

未在DIST中正确编译TypeScrip src模块导入

表单嵌套太深

ApexCharts条形图未显示最小值.负值

如何在NX工作区项目.json中设置类似angular.json的两个项目构建配置?

使用动态输出类型和泛型可重用接口提取对象属性

将具有Readonly数组属性的对象接口转换为数组

不带其他属性的精确函数返回类型

有没有办法防止类型交集绕过联合类型限制?

在Cypress中,是否可以在不标识错误的情况下对元素调用.Click()方法?

类型脚本-在调用期间解析函数参数类型

我不明白使用打字脚本在模板中展开参考

具有匹配功能的TS通用事件处理程序

定义一个只允许来自另一个类型的特定字符串文字的类型的最佳方式