简体   繁体   English

Typescript 错误:属性“status”在类型“never”上不存在。ts(2339)

[英]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.我目前有一个看起来像这样的方法,它使用nextjs/auth使用表单中的凭据登录。 However, I'm getting a type checking error Object is possibly 'undefined'.ts(2532)但是,我收到类型检查错误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.我(有点)明白为什么,如果 result == undefined 那么 result.status 可能是未定义的,嘿嘿错误。 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.然后我得到Property 'status' does not exist on type 'never'.ts(2339)所以我尝试隐式检查键if (result && 'status' in result && result.status === 200 && result.ok) {但这也不起作用。 Anyone know what I'm doing wrong here to get Typescript to play nice?任何人都知道我在这里做错了什么让 Typescript 玩得开心?

Under the hood, the definition of signIn (from third party next-auth.js looks like this btw:在引擎盖下,登录的定义(来自第三方 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>;

Update for Googlers.谷歌员工更新。

Wrong Response Type - SignInResponse not SignInOptions错误的响应类型 - SignInResponse 不是 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) {

I got stuck here too.我也卡在这里了。 Nothing seemed to make TypeScript happy here fully.似乎没有什么能让 TypeScript 在这里完全开心。

Your updated solution in the original description still gives me: "Property 'ok' does not exist on type 'never'."您在原始描述中更新的解决方案仍然给我:“'never' 类型不存在属性'ok'。” 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;

One way is to pass in RedirectableProviderType 'credentials' as a type一种方法是将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.

相关问题 TypeScript 错误:属性“scrollIntoView”在类型“never”上不存在。 TS2339 - TypeScript error: Property 'scrollIntoView' does not exist on type 'never'. TS2339 类型“从不”上不存在属性“单击”。 TS2339 - Property 'click' does not exist on type 'never'. TS2339 NextJs 中的 axios 响应中的类型“never”.ts(2339) 上不存在属性 - Property does not exist on type 'never'.ts(2339) in axios response in NextJs Typescript 通用:“T”类型上不存在属性“pass”.ts(2339) - Typescript generic : Property 'pass' does not exist on type 'T'.ts(2339) Angular 管道上的 .includes 返回“属性 &#39;includes&#39; 在类型 &#39;never&#39;.ts(2339)&#39; 上不存在错误消息 - .includes on Angular pipe is returning 'Property 'includes' does not exist on type 'never'.ts(2339)' error message 类型'A'.ts(2339)上不存在属性'ref' - React,TypeScript - Property 'ref' does not exist on type 'A'.ts(2339) - React, TypeScript "<i>TypeScript: Property &#39;data&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript:类型&#39;{ children?:ReactNode;上不存在属性&#39;data&#39;<\/b> <i>}&#39;.<\/i> }&#39;。<\/b> <i>ts(2339)<\/i> ts(2339)<\/b>" - TypeScript: Property 'data' does not exist on type '{ children?: ReactNode; }'. ts(2339) 打字稿错误TS2339:'Window'类型中不存在属性'webkitURL' - Typescript error TS2339: Property 'webkitURL' does not exist on type 'Window' TypeScript:错误TS2339:类型“ Window”上不存在属性“ CustomEvent” - TypeScript: error TS2339: Property 'CustomEvent' does not exist on type 'Window' 如何解决打字稿错误 TS2339:“IntrinsicAttributes &amp; …”类型上不存在“XXX”属性? - How to resolve typescript error TS2339: Property 'XXX' does not exist on type 'IntrinsicAttributes & …?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM