![](/img/trans.png)
[英]TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339
[英]Typescript error: Property 'status' does not exist on type 'never'.ts(2339)
我目前有一個看起來像這樣的方法,它使用nextjs/auth
使用表單中的憑據登錄。 但是,我收到類型檢查錯誤Object is possibly '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 玩得開心?
在引擎蓋下,登錄的定義(來自第三方 next-auth.js 看起來像這樣順便說一句:
export declare function signIn<P extends RedirectableProviderType | undefined = undefined>(provider?: LiteralUnion<BuiltInProviderType>, options?: SignInOptions, authorizationParams?: SignInAuthorisationParams): Promise<P extends RedirectableProviderType ? SignInResponse | undefined : undefined>;
谷歌員工更新。
錯誤的響應類型 - 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;
一種方法是將RedirectableProviderType 'credentials' 作為類型傳入
const result: SignInResponse | undefined = await signIn<'credentials'>(
'credentials',
{
redirect: false,
email: enteredEmail,
password: enteredPassword,
}
);
if (!!result && result.error) {
// set some auth state
router.replace('/account');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.