-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Issue with unions and instanceof requiring left hand side to be type of any #2775
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
Comments
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) { } |
I agree, as long as at least one constituent satisfies that rule, it should be allowed. |
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? |
And on @RyanCavanaugh's example, I'm not getting any errors on master |
Ok, it appears to be fixed in master. |
Today we had this issue:
The problem was caused by this code:
The signature in the Json.parse object states that it returns a type The version of our compiler is: |
+1 |
@markdrake what you need is a pair of parens: if (!(parsedJson instanceof Array)) {
parsedJson = [parsedJson];
}
|
great! tanks! |
@mhegazy Thank you so much |
😫 |
With
in a
|
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' ) |
Filed #19298 to track that. |
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:
The typeof works fine and correctly shows value as a string within the guarded block. The following line has an error though:
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.
The text was updated successfully, but these errors were encountered: