簡體   English   中英

CustomError類型的參數不能分配給Typescript中的Error

[英]Argument of type CustomError is not assignable to Error in Typescript

為什么我的打字稿引用我的自定義錯誤給我錯誤Argument of type CustomError is not assignable to parameter of type Error in Typescript

我的錯誤課

module Errors {

    export declare class Error  {
            public name: string;
            public message: string;
            public stack: string;
            constructor(message?: string);
    }

    export class CustomError extends Error {
        constructor(message: string) {
            super(message);
            this.name = 'invalid parameters error';
            this.message = message || 'the parameters for the request or call are incorrect.';
            this.stack = (<any>new Error()).stack;
        }
    }
}

並在代碼中返回Sequelize的藍鳥承諾。

    var Promise = require('bluebird');
    var import Errors = require('./errors')

    //using fs here for an example, it can be any bluebird promise

    fs.readFileAsync("file.json").catch(Errors.InvalidParameterError, e => { //this is typescript compiler error
                return reply(Boom.badRequest())
            })

問題是lib.d.ts Error聲明為接口和變量,但不是類。

選項1.實現Error接口並通過util.inherits繼承

import util = require('util');

module Errors {
    export class CustomError implements Error {
        name: string;
        message: string;

        constructor(message: string) {
            Error.call(this);
            Error.captureStackTrace(this, this.constructor);
            this.name = 'invalid parameters error';
            this.message = message || 'the parameters for the request or call are incorrect.';
        }
    }

    util.inherits(CustomError, Error);
}

export = Errors;

注意, captureStackTrace未在默認聲明中聲明,因此您應該在onw .d.ts文件中聲明它:

interface ErrorConstructor {
    captureStackTrace(error: Error, errorConstructor: Function);
}

選項2.沒有糖類

module Errors {
    export var CustomError = <ErrorConstructor>function (message: string) {
        Error.call(this);
        Error.captureStackTrace(this, this.constructor);
        this.name = 'invalid parameters error';
        this.message = message || 'the parameters for the request or call are incorrect.';
    }

    util.inherits(CustomError, Error);
}

選項3.純TypeScript方式

聲明文件(由於鴨子打字不必要):

declare type ErrorInterface = Error;

錯誤模塊:

module Errors {
    declare class Error implements ErrorInterface {
        name: string;
        message: string;
        static captureStackTrace(object, objectConstructor?);
    }

    export class CustomError extends Error {
        constructor(message: string) {
            super();
            Error.captureStackTrace(this, this.constructor);
            this.name = 'invalid parameters error';
            this.message = message || 'the parameters for the request or call are incorrect.';
        }
    }
}

export = Errors;

還要檢查你是否有正確的捕獲聲明:

catch(e: Error) // wrong
catch(e: { new (message?: string): Error }) // right

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM