简体   繁体   English

“错误”类型上不存在属性“代码”

[英]Property 'code' does not exist on type 'Error'

How can i access the Error.code property?如何访问 Error.code 属性? I get a Typescript error because the property 'code' does not exist on type 'Error'.我收到 Typescript 错误,因为“错误”类型上不存在属性“代码”。

this.authCtrl.login(user, {
   provider: AuthProviders.Password,
   method: AuthMethods.Password
}).then((authData) => {
    //Success
}).catch((error) => {
   console.log(error); // I see a code property
   console.log(error.code); //error
})

Or is there another way to make custom error messages?还是有另一种方法来制作自定义错误消息? I want to show the errors in another language.我想用另一种语言显示错误。

The real issue is that the Node.js definition file isn't exporting a proper Error definition. 真正的问题是Node.js定义文件没有导出正确的错误定义。 It uses the following for Error (and doesn't export this): 它使用以下内容进行错误(并且不导出此内容):

interface Error {
    stack?: string;
}

The actual definition it exports is in the NodeJS namespace: 它导出的实际定义是在NodeJS名称空间中:

export interface ErrnoException extends Error {
    errno?: number;
    code?: string;
    path?: string;
    syscall?: string;
    stack?: string;
}

So the following typecast will work: 所以下面的类型转换会起作用:

.catch((error: NodeJS.ErrnoException) => {
    console.log(error);
    console.log(error.code);
})

This seems like a flaw in Node's definition, since it doesn't line up with what an object from new Error() actually contains. 这似乎是Node定义中的一个缺陷,因为它与新的Error()实际包含的对象不一致。 TypeScript will enforce the interface Error definition. TypeScript将强制执行接口错误定义。

You have to cast a type to the error param from catch ie 你必须将一个类型转换为来自catch的错误参数

.catch((error:any) => {
    console.log(error);
    console.log(error.code);
});

or you can access the code property directly in this manner 或者您可以通过这种方式直接访问代码属性

.catch((error) => {
    console.log(error);
    console.log(error['code']);
});
export default class ResponseError extends Error {
    code: number;
    message: string;
    response: {
        headers: { [key: string]: string; };
        body: string;
    };
}

As explained by @Brent in https://stackoverflow.com/a/49562477 this problem is caused by an incomplete type definition in the Typescript package (see https://github.com/microsoft/TypeScript/blob/ed6889cd5b61b9fa5156018362d867def18e281d/lib/lib.es5.d.ts#L1039-L1043 ). As explained by @Brent in https://stackoverflow.com/a/49562477 this problem is caused by an incomplete type definition in the Typescript package (see https://github.com/microsoft/TypeScript/blob/ed6889cd5b61b9fa5156018362d867def18e281d/lib/ lib.es5.d.ts#L1039-L1043 )。 People using ECMAScript 2015, or later, can also use module declarations to declare the missing properties.使用 ECMAScript 2015 或更高版本的人也可以使用模块声明来声明缺失的属性。 This can be done by placing the following code inside the @types/global.d.ts file.这可以通过将以下代码放在@types/global.d.ts文件中来完成。

declare interface Error {
  name: string
  message: string
  stack?: string
  code?: number | string
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:属性 auth 在 FirebaseApp 类型上不存在 - Error: Property auth does not exist on type FirebaseApp “WorkerActionPayload”类型上不存在属性“queueSid” - Property 'queueSid' does not exist on type 'WorkerActionPayload' “Firebase”类型不存在属性“firestore” - Property 'firestore' does not exist on type 'Firebase' 错误“属性‘switchmap’在类型‘Observable’上不存在<user | null> " 当保护管理路由时</user> - Error "Property 'switchmap' does not exist on type 'Observable<User | null>" when protecting admin route Angular/Firestore - “Where”查询返回错误“AngularFirestoreCollection 类型上不存在属性” - Angular / Firestore - 'Where' query returning error 'property does not exist on type AngularFirestoreCollection' 错误 TS2339:属性 'firebaseConfig' 在类型 '{ production: boolean; 上不存在; }' - error TS2339: Property 'firebaseConfig' does not exist on type '{ production: boolean; }' 'Observable' 类型上不存在属性 'then' <documentsnapshot<documentdata> > Angular Firebase </documentsnapshot<documentdata> - Property 'then' does not exist on type 'Observable<DocumentSnapshot<DocumentData>> Angular Firebase React TypeScript/Firebase - 类型“UserCredential”上不存在属性“_tokenResponse” - React TypeScript/Firebase -Property '_tokenResponse' does not exist on type 'UserCredential' TS2570 错误协助:属性“sendEmailVerification”在类型“Promise”上不存在<user> '. 您是否忘记使用“等待”?</user> - Assistance With TS2570 Error: Property 'sendEmailVerification' does not exist on type 'Promise<User>'. Did you forget to use 'await'? 属性“auth”在类型“typeof import ...”上不存在 firebase/auth - Property 'auth' does not exist on type 'typeof import..." firebase/auth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM