Skip to content

Extending the system Error class does not work when transpiling to ES5 #42389

@octogonz

Description

@octogonz

Bug Report

The system Error class cannot be extended without calling Object.setPrototypeOf() as a workaround, like this:

class FooError extends Error {
    constructor(m: string) {
        super(m);

        // Set the prototype explicitly.
        Object.setPrototypeOf(this, FooError.prototype);
    }
}

The TypeScript 2.1 release notes imply that this behavior is by design.

However when I proposed to implement this workaround in the ANTLR4 runtime library, they observed that Babel injects the workaround automatically. Thus they concluded that this is a TypeScript bug rather than a problem with their library.

Is there some reason why TypeScript's transpiler cannot use the same solution as Babel?

🔎 Search Terms

setPrototypeOf extends Error

🕗 Version & Regression Information

Repros in TypeScript 4.1.3, first introduced in TypeScript 2.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions