Skip to content
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

instanceof Error typeguard not functioning #1938

Closed
basarat opened this issue Feb 5, 2015 · 3 comments
Closed

instanceof Error typeguard not functioning #1938

basarat opened this issue Feb 5, 2015 · 3 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@basarat
Copy link
Contributor

basarat commented Feb 5, 2015

Wonder if this is something that should work. I think it should:

    try {
         throw new Error('awesome');
    } catch (ex) {
        if (ex instanceof Error) {
            console.log(ex.foo);  // compiles. Should be error
        }
    }

The reason is that ex is still of type any. I suspect its in the spec that instanceof guard will not work on type any (I haven't checked).

image

Would be great if we could have this though.

@RyanCavanaugh RyanCavanaugh added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Feb 5, 2015
@RyanCavanaugh
Copy link
Member

See #1426. We do not narrow values of type any.

The first version of type guards did narrow any values, but there was too much code in the wild that did stuff like

var x: any; // x is a string or an HTML element I know is an image
if(x instanceof HTMLElement) {
  x.width = 100;
}

@basarat
Copy link
Contributor Author

basarat commented Feb 5, 2015

@RyanCavanaugh sure its #1425 ?

@RyanCavanaugh
Copy link
Member

Typo'd and updated, meant #1426

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

2 participants