-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Disallow instantiation expressions on the right side of instanceof #53323
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
tests/cases/compiler/instanceofOnInstantiationExpression.ts(10,21): error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression. | ||
tests/cases/compiler/instanceofOnInstantiationExpression.ts(11,22): error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression. | ||
tests/cases/compiler/instanceofOnInstantiationExpression.ts(12,23): error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression. | ||
|
||
|
||
==== tests/cases/compiler/instanceofOnInstantiationExpression.ts (3 errors) ==== | ||
declare class Box<T> { | ||
value: T; | ||
} | ||
|
||
|
||
declare const maybeBox: unknown; | ||
|
||
maybeBox instanceof Box; // OK | ||
|
||
maybeBox instanceof Box<number>; // error | ||
~~~~~~~~~~~ | ||
!!! error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression. | ||
maybeBox instanceof (Box<number>); // error | ||
~~~~~~~~~~~ | ||
!!! error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression. | ||
maybeBox instanceof ((Box<number>)); // error | ||
~~~~~~~~~~~ | ||
!!! error TS2848: The right-hand side of an 'instanceof' expression must not be an instantiation expression. | ||
|
||
Box<number> instanceof Object; // OK | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am checking only the right side here, but, should these even be legal? They seem useless in another way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is the instantiation expression pointless? Yes. Is it harmful? No. Is it misleading? Probably not really on the LHS, since the only reason I can think of why you'd want one on the LHS is to "satisfy" the RHS, but the RHS forbids them now, soooo... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah. I don't really care either way. I guess this PR is fine as-is. |
||
(Box<number>) instanceof Object; // OK | ||
((Box<number>)) instanceof Object; // OK | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
//// [instanceofOnInstantiationExpression.ts] | ||
declare class Box<T> { | ||
value: T; | ||
} | ||
|
||
|
||
declare const maybeBox: unknown; | ||
|
||
maybeBox instanceof Box; // OK | ||
|
||
maybeBox instanceof Box<number>; // error | ||
maybeBox instanceof (Box<number>); // error | ||
maybeBox instanceof ((Box<number>)); // error | ||
|
||
Box<number> instanceof Object; // OK | ||
(Box<number>) instanceof Object; // OK | ||
((Box<number>)) instanceof Object; // OK | ||
|
||
|
||
//// [instanceofOnInstantiationExpression.js] | ||
maybeBox instanceof Box; // OK | ||
maybeBox instanceof (Box); // error | ||
maybeBox instanceof (Box); // error | ||
maybeBox instanceof ((Box)); // error | ||
(Box) instanceof Object; // OK | ||
(Box) instanceof Object; // OK | ||
((Box)) instanceof Object; // OK |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
=== tests/cases/compiler/instanceofOnInstantiationExpression.ts === | ||
declare class Box<T> { | ||
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0)) | ||
>T : Symbol(T, Decl(instanceofOnInstantiationExpression.ts, 0, 18)) | ||
|
||
value: T; | ||
>value : Symbol(Box.value, Decl(instanceofOnInstantiationExpression.ts, 0, 22)) | ||
>T : Symbol(T, Decl(instanceofOnInstantiationExpression.ts, 0, 18)) | ||
} | ||
|
||
|
||
declare const maybeBox: unknown; | ||
>maybeBox : Symbol(maybeBox, Decl(instanceofOnInstantiationExpression.ts, 5, 13)) | ||
|
||
maybeBox instanceof Box; // OK | ||
>maybeBox : Symbol(maybeBox, Decl(instanceofOnInstantiationExpression.ts, 5, 13)) | ||
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0)) | ||
|
||
maybeBox instanceof Box<number>; // error | ||
>maybeBox : Symbol(maybeBox, Decl(instanceofOnInstantiationExpression.ts, 5, 13)) | ||
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0)) | ||
|
||
maybeBox instanceof (Box<number>); // error | ||
>maybeBox : Symbol(maybeBox, Decl(instanceofOnInstantiationExpression.ts, 5, 13)) | ||
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0)) | ||
|
||
maybeBox instanceof ((Box<number>)); // error | ||
>maybeBox : Symbol(maybeBox, Decl(instanceofOnInstantiationExpression.ts, 5, 13)) | ||
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0)) | ||
|
||
Box<number> instanceof Object; // OK | ||
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
(Box<number>) instanceof Object; // OK | ||
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
||
((Box<number>)) instanceof Object; // OK | ||
>Box : Symbol(Box, Decl(instanceofOnInstantiationExpression.ts, 0, 0)) | ||
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --)) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
=== tests/cases/compiler/instanceofOnInstantiationExpression.ts === | ||
declare class Box<T> { | ||
>Box : Box<T> | ||
|
||
value: T; | ||
>value : T | ||
} | ||
|
||
|
||
declare const maybeBox: unknown; | ||
>maybeBox : unknown | ||
|
||
maybeBox instanceof Box; // OK | ||
>maybeBox instanceof Box : boolean | ||
>maybeBox : unknown | ||
>Box : typeof Box | ||
|
||
maybeBox instanceof Box<number>; // error | ||
>maybeBox instanceof Box<number> : boolean | ||
>maybeBox : unknown | ||
>Box<number> : { new (): Box<number>; prototype: Box<any>; } | ||
>Box : typeof Box | ||
|
||
maybeBox instanceof (Box<number>); // error | ||
>maybeBox instanceof (Box<number>) : boolean | ||
>maybeBox : unknown | ||
>(Box<number>) : { new (): Box<number>; prototype: Box<any>; } | ||
>Box<number> : { new (): Box<number>; prototype: Box<any>; } | ||
>Box : typeof Box | ||
|
||
maybeBox instanceof ((Box<number>)); // error | ||
>maybeBox instanceof ((Box<number>)) : boolean | ||
>maybeBox : unknown | ||
>((Box<number>)) : { new (): Box<number>; prototype: Box<any>; } | ||
>(Box<number>) : { new (): Box<number>; prototype: Box<any>; } | ||
>Box<number> : { new (): Box<number>; prototype: Box<any>; } | ||
>Box : typeof Box | ||
|
||
Box<number> instanceof Object; // OK | ||
>Box<number> instanceof Object : boolean | ||
>Box<number> : { new (): Box<number>; prototype: Box<any>; } | ||
>Box : typeof Box | ||
>Object : ObjectConstructor | ||
|
||
(Box<number>) instanceof Object; // OK | ||
>(Box<number>) instanceof Object : boolean | ||
>(Box<number>) : { new (): Box<number>; prototype: Box<any>; } | ||
>Box<number> : { new (): Box<number>; prototype: Box<any>; } | ||
>Box : typeof Box | ||
>Object : ObjectConstructor | ||
|
||
((Box<number>)) instanceof Object; // OK | ||
>((Box<number>)) instanceof Object : boolean | ||
>((Box<number>)) : { new (): Box<number>; prototype: Box<any>; } | ||
>(Box<number>) : { new (): Box<number>; prototype: Box<any>; } | ||
>Box<number> : { new (): Box<number>; prototype: Box<any>; } | ||
>Box : typeof Box | ||
>Object : ObjectConstructor | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
declare class Box<T> { | ||
value: T; | ||
} | ||
|
||
|
||
declare const maybeBox: unknown; | ||
|
||
maybeBox instanceof Box; // OK | ||
|
||
maybeBox instanceof Box<number>; // error | ||
maybeBox instanceof (Box<number>); // error | ||
maybeBox instanceof ((Box<number>)); // error | ||
|
||
Box<number> instanceof Object; // OK | ||
(Box<number>) instanceof Object; // OK | ||
((Box<number>)) instanceof Object; // OK |
Uh oh!
There was an error while loading. Please reload this page.