TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

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

Open Anupheaus opened this issue 11 years ago • 14 comments

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.

Anupheaus avatar Apr 15 '15 09:04 Anupheaus

This should just work. It doesn't make sense that every component of a union type should have to be an object type.

class Bar { n: number; }
class Foo { s: string; }

// Error, but shouldn't be
var x: Bar|number;
if(x instanceof Foo) { }

// No error
var y: Bar|Foo;
if(y instanceof Foo) { }

RyanCavanaugh avatar Apr 16 '15 20:04 RyanCavanaugh

I agree, as long as at least one constituent satisfies that rule, it should be allowed.

JsonFreeman avatar Apr 17 '15 01:04 JsonFreeman

I tried this code, and I'm actually not getting an error on the instanceof check. However, the type is not narrowing correctly in the instanceof block:

class CString {
    cStringMember: any;
}

var value: CString | string;
if (value instanceof CString) { // No error
    value.cStringMember; // Error because value has type CString | string
}
else if (typeof value === "string") {
    value = "";
}

@Anupheaus are you on master?

JsonFreeman avatar Apr 17 '15 20:04 JsonFreeman

And on @RyanCavanaugh's example, I'm not getting any errors on master

JsonFreeman avatar Apr 17 '15 20:04 JsonFreeman

Ok, it appears to be fixed in master.

JsonFreeman avatar Apr 17 '15 20:04 JsonFreeman

Today we had this issue:

src/app/file.ts(26,13): error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.

The problem was caused by this code:

let parsedJson:any = JSON.parse(json);

if (!parsedJson instanceof Array) {
     parsedJson = [parsedJson];
}

The signature in the Json.parse object states that it returns a type any so we don't understand why the union is not possible. However the compiled file works just fine but we don't want to have false warnings in our compile process or it will become really messy with time. What can we do about it?

The version of our compiler is: message TS6029: Version 1.7.5. Thanks in advance.

markdrake avatar Jan 08 '16 22:01 markdrake

+1

rrgarciach avatar Jan 08 '16 22:01 rrgarciach

@markdrake what you need is a pair of parens:

if (!(parsedJson instanceof Array)) {
     parsedJson = [parsedJson];
}

! has a higher precedence than instanceof operator. so what you were doing is (!parsedJson) instanceof Array; which is invalid.

mhegazy avatar Jan 09 '16 00:01 mhegazy

great! tanks!

rrgarciach avatar Jan 09 '16 01:01 rrgarciach

@mhegazy Thank you so much

markdrake avatar Jan 09 '16 01:01 markdrake

! has a higher precedence than instanceof operator.

:tired_face:

DanielRosenwasser avatar Jan 09 '16 05:01 DanielRosenwasser

With [email protected] I'm getting

 TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.

in a

if (this[key] instanceof Function) { }

larsenwork avatar Aug 21 '17 08:08 larsenwork

I repro @larsenwork's issue with the following:

type CreatedOrUpdatedAt =
    { createdAt: Date | string } |
    { updatedAt: Date | string };

const datePropComp = <T extends CreatedOrUpdatedAt, K extends keyof T>(a: T, b: T, field: K) => {
    // The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.
    const dateA = a[field] instanceof Date ? a[field] : new Date(a[field]);
    const dateB = b[field] instanceof Date ? b[field] : new Date(b[field]);

    return dateA.getTime() - dateB.getTime();
};

datePropComp({ updatedAt: Date() }, { updatedAt: Date() }, 'updatedAt' )

Playground link

yangchristian avatar Oct 18 '17 05:10 yangchristian

Filed https://github.com/Microsoft/TypeScript/issues/19298 to track that.

mhegazy avatar Oct 18 '17 16:10 mhegazy