Skip to content

instanceof operator doesn't work properly since 2.1.4 #13896

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dvabuzyarov opened this issue Feb 6, 2017 · 2 comments
Closed

instanceof operator doesn't work properly since 2.1.4 #13896

dvabuzyarov opened this issue Feb 6, 2017 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@dvabuzyarov
Copy link

TypeScript Version: 2.1.5

Code

export class HttpError extends Error {
    constructor() {
        super();
    }
}

export class HttpLoginError extends HttpError {
    constructor() {
        super();
    }
}

const error = new HttpLoginError();

Expected behavior:
(error instanceof HttpLoginError); //returns true
(error instanceof HttpError); //returns true
(error instanceof Error); //returns true

Actual behavior:
(error instanceof HttpLoginError); //returns false
(error instanceof HttpError); //returns false
(error instanceof Error); //returns true

@dvabuzyarov dvabuzyarov changed the title instanceof operator doesn't work property since 2.1.4 instanceof operator doesn't work properly since 2.1.4 Feb 6, 2017
@DanielRosenwasser
Copy link
Member

This was an intentional change in 2.2 (see #12123 and the section on our wiki), but is difficult to overcome through compilation. I believe there's some conversation in #12790 for workarounds.

A workaround you can take now is create an intermediate class that you can extend from.

export interface MyErrorStatic {
    new (message?: string): RxError;
}
export interface MyError extends Error {}

export const MyError: MyErrorStatic = function MyError(this: Error, message: string) {
    const err = Error.call(this, message);
    this.message = message;
    this.stack = err.stack;
    return err;
} as any;

export class HttpError extends MyError {
    // ...
}

In TypeScript 2.2, you'll be able to set the prototype on your own.

// Use this class to correct the prototype chain.
export class MyError extends Error {
    __proto__: Error;
    constructor(message?: string) {
        const trueProto = new.target.prototype;
        super(message);

        // Alternatively use Object.setPrototypeOf if you have an ES6 environment.
        this.__proto__ = trueProto;
    }
}

@DanielRosenwasser DanielRosenwasser added the Duplicate An existing issue was already created label Feb 6, 2017
@jimmykane
Copy link

Sorry but I am having a similar issue with latest Typescript can you check the following @DanielRosenwasser

I construct the following

screen shot 2018-05-15 at 20 57 55

as you can see that Data I have is an instance of Altitude but they only refer to number

on a second page reload this changes and becomes vise versa

screen shot 2018-05-15 at 21 03 16

@microsoft microsoft locked and limited conversation to collaborators Jul 31, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants