Skip to content

Error when LHS of instanceof is Union of Primitives #18519 #19063

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

Merged
merged 3 commits into from
Nov 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17587,6 +17587,12 @@ namespace ts {
(kind & TypeFlags.NonPrimitive && isTypeAssignableTo(source, nonPrimitiveType));
}

function allTypesAssignableToKind(source: Type, kind: TypeFlags, strict?: boolean): boolean {
return source.flags & TypeFlags.Union ?
every((source as UnionType).types, subType => allTypesAssignableToKind(subType, kind, strict)) :
isTypeAssignableToKind(source, kind, strict);
}

function isConstEnumObjectType(type: Type): boolean {
return getObjectFlags(type) & ObjectFlags.Anonymous && type.symbol && isConstEnumSymbol(type.symbol);
}
Expand All @@ -17604,7 +17610,8 @@ namespace ts {
// and the right operand to be of type Any, a subtype of the 'Function' interface type, or have a call or construct signature.
// The result is always of the Boolean primitive type.
// NOTE: do not raise error if leftType is unknown as related error was already reported
if (!isTypeAny(leftType) && isTypeAssignableToKind(leftType, TypeFlags.Primitive)) {
if (!isTypeAny(leftType) &&
allTypesAssignableToKind(leftType, TypeFlags.Primitive)) {
error(left, Diagnostics.The_left_hand_side_of_an_instanceof_expression_must_be_of_type_any_an_object_type_or_a_type_parameter);
}
// NOTE: do not raise error if right is unknown as related error was already reported
Expand Down
21 changes: 21 additions & 0 deletions tests/baselines/reference/instanceofWithPrimitiveUnion.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
tests/cases/compiler/instanceofWithPrimitiveUnion.ts(2,9): error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.
tests/cases/compiler/instanceofWithPrimitiveUnion.ts(8,9): error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.


==== tests/cases/compiler/instanceofWithPrimitiveUnion.ts (2 errors) ====
function test1(x: number | string) {
if (x instanceof Object) {
~
!!! error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.
x;
}
}

function test2(x: (number | string) | number) {
if (x instanceof Object) {
~
!!! error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.
x;
}
}

25 changes: 25 additions & 0 deletions tests/baselines/reference/instanceofWithPrimitiveUnion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [instanceofWithPrimitiveUnion.ts]
function test1(x: number | string) {
if (x instanceof Object) {
x;
}
}

function test2(x: (number | string) | number) {
if (x instanceof Object) {
x;
}
}


//// [instanceofWithPrimitiveUnion.js]
function test1(x) {
if (x instanceof Object) {
x;
}
}
function test2(x) {
if (x instanceof Object) {
x;
}
}
27 changes: 27 additions & 0 deletions tests/baselines/reference/instanceofWithPrimitiveUnion.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
=== tests/cases/compiler/instanceofWithPrimitiveUnion.ts ===
function test1(x: number | string) {
>test1 : Symbol(test1, Decl(instanceofWithPrimitiveUnion.ts, 0, 0))
>x : Symbol(x, Decl(instanceofWithPrimitiveUnion.ts, 0, 15))

if (x instanceof Object) {
>x : Symbol(x, Decl(instanceofWithPrimitiveUnion.ts, 0, 15))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

x;
>x : Symbol(x, Decl(instanceofWithPrimitiveUnion.ts, 0, 15))
}
}

function test2(x: (number | string) | number) {
>test2 : Symbol(test2, Decl(instanceofWithPrimitiveUnion.ts, 4, 1))
>x : Symbol(x, Decl(instanceofWithPrimitiveUnion.ts, 6, 15))

if (x instanceof Object) {
>x : Symbol(x, Decl(instanceofWithPrimitiveUnion.ts, 6, 15))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

x;
>x : Symbol(x, Decl(instanceofWithPrimitiveUnion.ts, 6, 15))
}
}

29 changes: 29 additions & 0 deletions tests/baselines/reference/instanceofWithPrimitiveUnion.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
=== tests/cases/compiler/instanceofWithPrimitiveUnion.ts ===
function test1(x: number | string) {
>test1 : (x: string | number) => void
>x : string | number

if (x instanceof Object) {
>x instanceof Object : boolean
>x : string | number
>Object : ObjectConstructor

x;
>x : string | number
}
}

function test2(x: (number | string) | number) {
>test2 : (x: string | number) => void
>x : string | number

if (x instanceof Object) {
>x instanceof Object : boolean
>x : string | number
>Object : ObjectConstructor

x;
>x : string | number
}
}

11 changes: 11 additions & 0 deletions tests/cases/compiler/instanceofWithPrimitiveUnion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function test1(x: number | string) {
if (x instanceof Object) {
x;
}
}

function test2(x: (number | string) | number) {
if (x instanceof Object) {
x;
}
}