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

Conversation

charlespierce
Copy link
Contributor

Fixes #18519

Added additional check for if the left-hand operand of an instanceof is a union of primitives and raise an error in that case as well.

@mhegazy
Copy link
Contributor

mhegazy commented Oct 10, 2017

@DanielRosenwasser can you review this change.

@charlespierce
Copy link
Contributor Author

@DanielRosenwasser @mhegazy Just wanted to check if there were any issues with this PR, so I can make any needed changes before it goes stale.

@@ -17604,7 +17604,9 @@ 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) &&
(isTypeAssignableToKind(leftType, TypeFlags.Primitive) ||
Copy link
Member

@DanielRosenwasser DanielRosenwasser Nov 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it makes sense to have isTypeAssignableToKind just work for all constituents if given a union type. Perhaps make an allTypesAssignableToKind helper below it to keep the change scoped.

Otherwise, this seems fine.

@charlespierce
Copy link
Contributor Author

@DanielRosenwasser I like keeping the change scoped, since isTypeAssignableToKind is used in a lot of places and there may be some unintended consequences of adding support for union types to it. Updated to use an allTypesAssignableToKind helper.

Also updated the test to include the case (number | string) | number, which would have failed in the previous implementation since it wasn't recursively checking for union types.

@@ -17587,6 +17587,16 @@ namespace ts {
(kind & TypeFlags.NonPrimitive && isTypeAssignableTo(source, nonPrimitiveType));
}

function allTypesAssignableToKind(source: Type, kind: TypeFlags, strict?: boolean): boolean {
if (source.flags & TypeFlags.Union) {
Copy link
Contributor

@mhegazy mhegazy Nov 6, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

@charlespierce
Copy link
Contributor Author

@mhegazy @DanielRosenwasser Any additional concerns?

@mhegazy mhegazy closed this Nov 28, 2017
@mhegazy mhegazy reopened this Nov 28, 2017
@mhegazy mhegazy merged commit 72da4b8 into microsoft:master Nov 28, 2017
@mhegazy
Copy link
Contributor

mhegazy commented Nov 28, 2017

Sorry for the delay.

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants