gpt4 book ai didi

javascript - typescript 错误 : Property 'status' does not exist on type 'never' . ts(2339)

转载 作者:行者123 更新时间:2023-12-05 05:55:12 25 4
gpt4 key购买 nike

我目前有一个看起来像这样的方法,它使用 nextjs/auth 使用表单中的凭据登录。但是,我收到类型检查错误 Object is possible 'undefined'.ts(2532)

const doStuff = async (values: any) => {
const result: SignInOptions | undefined = await signIn('credentials', {
redirect: false,
password: values.pass,
email: values.email,
});
if (result.status === 200 && result.ok) {
await asyncDispatcher(
loginUser({
email: values.email,
password: values.pass,
}),
);
router.push(props.redirectLocation);
}
};

我(有点)理解为什么,如果 result == undefined 那么 result.status 可能是未定义的,嘿嘿错误。美好的。那么我试试这个:

const doStuff = async (values: any) => {
const result: SignInOptions | undefined = await signIn('credentials', {
redirect: false,
password: values.pass,
email: values.email,
});
if (result && result.status === 200 && result.ok) {
await asyncDispatcher(
loginUser({
email: values.email,
password: values.pass,
}),
);
router.push(props.redirectLocation);
}
};

然后我得到 Property 'status' does not exist on type 'never'.ts(2339) 然后我尝试隐式检查键 if (result && 'status' in result && result.status === 200 && result.ok) { 但这也不起作用。任何人都知道我在这里做错了什么让 Typescript 发挥得很好?

在幕后,signIn 的定义(来自第三方 next-auth.js 看起来像这样 btw:

export declare function signIn<P extends RedirectableProviderType | undefined = undefined>(provider?: LiteralUnion<BuiltInProviderType>, options?: SignInOptions, authorizationParams?: SignInAuthorisationParams): Promise<P extends RedirectableProviderType ? SignInResponse | undefined : undefined>;

Google 员工更新。

错误的响应类型 - SignInResponse 不是 SignInOptions

解决方法:

const result: SignInResponse | undefined = await signIn<'credentials'>('credentials', {
redirect: false,
password: values.pass,
email: values.email,
});
if (result && 'status' in result && result.status === 200 && result.ok) {

最佳答案

我也卡在这里了。似乎没有什么能让 TypeScript 完全满意。

您在原始描述中更新的解决方案仍然给我:“类型‘never’上不存在属性‘ok’。”错误。

这是唯一对我有用的东西:

import type {SignInResponse} from 'next-auth/react';
import {signIn} from 'next-auth/react';

const result = await signIn('credentials', {
email: data.email,
password: data.password,
redirect: false,
}) as unknown as SignInResponse;

关于javascript - typescript 错误 : Property 'status' does not exist on type 'never' . ts(2339),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69492582/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com