Skip to content

Narrow intersected classes using instanceof #51032

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

Closed
wants to merge 1 commit into from
Closed
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
13 changes: 8 additions & 5 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17775,7 +17775,7 @@ namespace ts {
target.flags & TypeFlags.Union ? some((target as UnionType).types, t => isTypeDerivedFrom(source, t)) :
source.flags & TypeFlags.InstantiableNonPrimitive ? isTypeDerivedFrom(getBaseConstraintOfType(source) || unknownType, target) :
target === globalObjectType ? !!(source.flags & (TypeFlags.Object | TypeFlags.NonPrimitive)) :
target === globalFunctionType ? !!(source.flags & TypeFlags.Object) && isFunctionObjectType(source as ObjectType) :
target === globalFunctionType ? isFunctionObjectType(source) :
Copy link
Member

Choose a reason for hiding this comment

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

The issue isn't just with x instanceof Function checks, it also extends to x instanceof Object checks. I have a more general solution to the problem in #51631.

hasBaseType(source, getTargetType(target)) || (isArrayType(target) && !isReadonlyArrayType(target) && isTypeDerivedFrom(source, globalReadonlyArrayType));
}

Expand Down Expand Up @@ -23945,10 +23945,13 @@ namespace ts {
return isTypeAssignableTo(assignedType, reducedType) ? reducedType : declaredType;
}

function isFunctionObjectType(type: ObjectType): boolean {
function isFunctionObjectType(type: Type): boolean {
if (!(type.flags & TypeFlags.StructuredType)) {
return false;
}
// We do a quick check for a "bind" property before performing the more expensive subtype
// check. This gives us a quicker out in the common case where an object type is not a function.
const resolved = resolveStructuredTypeMembers(type);
const resolved = resolveStructuredTypeMembers(type as StructuredType);
return !!(resolved.callSignatures.length || resolved.constructSignatures.length ||
resolved.members.get("bind" as __String) && isTypeSubtypeOf(type, globalFunctionType));
}
Expand Down Expand Up @@ -23996,7 +23999,7 @@ namespace ts {
if (flags & TypeFlags.Object) {
return getObjectFlags(type) & ObjectFlags.Anonymous && isEmptyObjectType(type as ObjectType) ?
strictNullChecks ? TypeFacts.EmptyObjectStrictFacts : TypeFacts.EmptyObjectFacts :
isFunctionObjectType(type as ObjectType) ?
isFunctionObjectType(type) ?
strictNullChecks ? TypeFacts.FunctionStrictFacts : TypeFacts.FunctionFacts :
strictNullChecks ? TypeFacts.ObjectStrictFacts : TypeFacts.ObjectFacts;
}
Expand Down Expand Up @@ -34629,7 +34632,7 @@ namespace ts {
declKind !== AssignmentDeclarationKind.ModuleExports &&
declKind !== AssignmentDeclarationKind.Prototype &&
!isEmptyObjectType(rightType) &&
!isFunctionObjectType(rightType as ObjectType) &&
!isFunctionObjectType(rightType) &&
!(getObjectFlags(rightType) & ObjectFlags.Class)) {
// don't check assignability of module.exports=, C.prototype=, or expando types because they will necessarily be incomplete
checkAssignmentOperator(rightType);
Expand Down
36 changes: 36 additions & 0 deletions tests/baselines/reference/instanceofNarrowIntersection.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
=== tests/cases/compiler/instanceofNarrowIntersection.ts ===
// repro #50844

interface InstanceOne {
>InstanceOne : Symbol(InstanceOne, Decl(instanceofNarrowIntersection.ts, 0, 0))

one(): void
>one : Symbol(InstanceOne.one, Decl(instanceofNarrowIntersection.ts, 2, 23))
}
interface InstanceTwo {
>InstanceTwo : Symbol(InstanceTwo, Decl(instanceofNarrowIntersection.ts, 4, 1))

two(): void
>two : Symbol(InstanceTwo.two, Decl(instanceofNarrowIntersection.ts, 5, 23))
}

declare const instance: InstanceOne | InstanceTwo
>instance : Symbol(instance, Decl(instanceofNarrowIntersection.ts, 9, 13))
>InstanceOne : Symbol(InstanceOne, Decl(instanceofNarrowIntersection.ts, 0, 0))
>InstanceTwo : Symbol(InstanceTwo, Decl(instanceofNarrowIntersection.ts, 4, 1))

declare const SomeCls: { new (): InstanceOne } & { foo: true }
>SomeCls : Symbol(SomeCls, Decl(instanceofNarrowIntersection.ts, 10, 13))
>InstanceOne : Symbol(InstanceOne, Decl(instanceofNarrowIntersection.ts, 0, 0))
>foo : Symbol(foo, Decl(instanceofNarrowIntersection.ts, 10, 50))

if (instance instanceof SomeCls) {
>instance : Symbol(instance, Decl(instanceofNarrowIntersection.ts, 9, 13))
>SomeCls : Symbol(SomeCls, Decl(instanceofNarrowIntersection.ts, 10, 13))

instance.one()
>instance.one : Symbol(InstanceOne.one, Decl(instanceofNarrowIntersection.ts, 2, 23))
>instance : Symbol(instance, Decl(instanceofNarrowIntersection.ts, 9, 13))
>one : Symbol(InstanceOne.one, Decl(instanceofNarrowIntersection.ts, 2, 23))
}

32 changes: 32 additions & 0 deletions tests/baselines/reference/instanceofNarrowIntersection.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
=== tests/cases/compiler/instanceofNarrowIntersection.ts ===
// repro #50844

interface InstanceOne {
one(): void
>one : () => void
}
interface InstanceTwo {
two(): void
>two : () => void
}

declare const instance: InstanceOne | InstanceTwo
>instance : InstanceOne | InstanceTwo

declare const SomeCls: { new (): InstanceOne } & { foo: true }
>SomeCls : (new () => InstanceOne) & { foo: true; }
>foo : true
>true : true

if (instance instanceof SomeCls) {
>instance instanceof SomeCls : boolean
>instance : InstanceOne | InstanceTwo
>SomeCls : (new () => InstanceOne) & { foo: true; }

instance.one()
>instance.one() : void
>instance.one : () => void
>instance : InstanceOne
>one : () => void
}

18 changes: 18 additions & 0 deletions tests/cases/compiler/instanceofNarrowIntersection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @strict: true
// @noEmit: true

// repro #50844

interface InstanceOne {
one(): void
}
interface InstanceTwo {
two(): void
}

declare const instance: InstanceOne | InstanceTwo
declare const SomeCls: { new (): InstanceOne } & { foo: true }

if (instance instanceof SomeCls) {
instance.one()
}