Skip to content

instanceof on union type incorrectly issues error #1785

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
epic0tix opened this issue Jan 23, 2015 · 4 comments · Fixed by #1803
Closed

instanceof on union type incorrectly issues error #1785

epic0tix opened this issue Jan 23, 2015 · 4 comments · Fixed by #1803
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@epic0tix
Copy link

The below statement does not work as it wants the left side of the instanceof statement to be of type any, which kind of defeats the purpose as once you do that TS will not longer use typegaurds, or am I missing something

constructor(template: string|templateMap){

           if (template instanceof templateMap) {
                con = super.getPaneContainer(template.selector);
            } else {
                con = super.getPaneContainer('.ui-layout-center');
            }

}
@epic0tix
Copy link
Author

if you get the string check out of the way before you get to the instanceof check it works just fine

            if (typeof template === 'string') {
                con = super.getPaneContainer('.ui-layout-center');
            }
            else if (template instanceof templateMap) {
                con = super.getPaneContainer(template.selector);
            } else {
                con = super.getPaneContainer('.ui-layout-center');
            }

@RyanCavanaugh
Copy link
Member

Is this a TSLint issue?

@epic0tix
Copy link
Author

It is happening in vs2013 update 3 typescript 1.4

@RyanCavanaugh RyanCavanaugh changed the title linter complains about typeguard usage while using instanceof of instanceof on union type incorrectly issues error Jan 23, 2015
@RyanCavanaugh
Copy link
Member

To simplify:

var x: string|HTMLElement;

// Error, but shouldn't be
if(x instanceof HTMLElement) {

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants