Skip to content

Issue with unions and instanceof requiring left hand side to be type of any #2775

Closed
@Anupheaus

Description

@Anupheaus

I apologise in advance if this has been brought up somewhere else, I did do a quick search but couldn't really find it anywhere. I have the following code:

public get message() { return this._message; }
public set message(value: CString | string) {
    if (value instanceof CString) {
        this._message = isCString(value, this._message);
    } else if (typeof value === 'string') {
        this._message.setValue(value, app.languages.English);
    }
}

The typeof works fine and correctly shows value as a string within the guarded block. The following line has an error though:

if (value instanceof CString) {

The value is underlined with the following message:
"The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter."

I suspect that this is because of the reason specified in this stack overflow question but it seems that this would happen so often, is there any workaround or something that can be done inside TypeScript to get around this? Technically it'll work just fine as JavaScript, it's just the intellisense/interpreter raises an error here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptFixedA PR has been merged for this issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions