'Typescript error: Property 'status' does not exist on type 'never'.ts(2339)
I've currently got a method that looks like this, which uses nextjs/auth to signin with credentials from a form. However, I'm getting a type checking error 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);
}
};
I (sortof) understand why, if result == undefined then result.status could potentially be undefined and hey ho ERROR. Fine. So then I try this:
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);
}
};
I then get Property 'status' does not exist on type 'never'.ts(2339) so then I try to implicitly check for the keys if (result && 'status' in result && result.status === 200 && result.ok) { but this doesn't work either. Anyone know what I'm doing wrong here to get Typescript to play nice?
Under the hood, the definition of signIn (from third party next-auth.js looks like this btw:
export declare function signIn<P extends RedirectableProviderType | undefined = undefined>(provider?: LiteralUnion<BuiltInProviderType>, options?: SignInOptions, authorizationParams?: SignInAuthorisationParams): Promise<P extends RedirectableProviderType ? SignInResponse | undefined : undefined>;
Update for Googlers.
Wrong Response Type - SignInResponse not SignInOptions
Solution:
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) {
Solution 1:[1]
I got stuck here too. Nothing seemed to make TypeScript happy here fully.
Your updated solution in the original description still gives me: "Property 'ok' does not exist on type 'never'." errors.
This was the only thing that worked for me:
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;
Solution 2:[2]
One way is to pass in RedirectableProviderType 'credentials' as a type
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');
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ev Haus |
| Solution 2 | abdul wahab Shahid |
