Skip to content

TypeScript 2.1.x fails to extend Error #12790

@jamesadarich

Description

@jamesadarich

TypeScript Version: 2.1.4

Code

Given the following definition

export default class ExtendedError extends Error {
    public get something(): number {
        return 42;
    }

    public doSomething() {

    }

}

I get the following output for 2.0.3

const extendedErrorInstance = new ExtendedError();

console.log("Is instanceof ExtendedError:", extendedErrorInstance instanceof ExtendedError);
// outputs "Is instanceof ExtendedError: true"
console.log("Is instanceof Error:", extendedErrorInstance instanceof Error);
// outputs "Is instanceof Error: true"
console.log("Value of something:", extendedErrorInstance.something);
// outputs "Value of something: 42"
console.log("Do something:", extendedErrorInstance.doSomething());
// executes function and outputs "Do something: undefined"

However for version 2.1.1 and 2.1.4 I get the following

const extendedErrorInstance = new ExtendedError();

console.log("Is instanceof ExtendedError:", extendedErrorInstance instanceof ExtendedError);
// outputs "Is instanceof ExtendedError: false"
console.log("Is instanceof Error:", extendedErrorInstance instanceof Error);
// outputs "Is instanceof Error: true"
console.log("Value of something:", extendedErrorInstance.something);
// outputs "Value of something: undefined"
console.log("Do something:", extendedErrorInstance.doSomething());
// throws error "TypeError: extendedErrorInstance.doSomething is not a function"

Expected behavior:
The extended error to behave as in the first example

Actual behavior:
It seems as if the error has not been extended at all

Thanks very much for your assistance in advance.

James

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing codeDuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions