Skip to content

Implement constructor check type guard #32256

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
49 changes: 49 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17247,6 +17247,12 @@ namespace ts {
if (right.kind === SyntaxKind.TypeOfExpression && isStringLiteralLike(left)) {
return narrowTypeByTypeof(type, <TypeOfExpression>right, operator, left, assumeTrue);
}
if (isConstructorAccessExpression(left) && right.kind === SyntaxKind.Identifier) {
return narrowTypeByConstructor(type, left, operator, right, assumeTrue);
}
if (isConstructorAccessExpression(right) && left.kind === SyntaxKind.Identifier) {
return narrowTypeByConstructor(type, right, operator, left, assumeTrue);
}
if (isMatchingReference(reference, left)) {
return narrowTypeByEquality(type, operator, right, assumeTrue);
}
Expand Down Expand Up @@ -17275,6 +17281,13 @@ namespace ts {
return narrowType(type, expr.right, assumeTrue);
}
return type;

function isConstructorAccessExpression(expr: Expression): expr is AccessExpression {
return (
isPropertyAccessEntityNameExpression(expr) && idText(expr.name) === "constructor"
|| isElementAccessExpression(expr) && isStringLiteralLike(expr.argumentExpression) && expr.argumentExpression.text === "constructor"
);
}
}

function narrowTypeByEquality(type: Type, operator: SyntaxKind, value: Expression, assumeTrue: boolean): Type {
Expand Down Expand Up @@ -17505,6 +17518,42 @@ namespace ts {
return getTypeWithFacts(mapType(type, narrowTypeForTypeofSwitch(impliedType)), switchFacts);
}

function narrowTypeByConstructor(type: Type, constructorAccessExpr: AccessExpression, operator: SyntaxKind, identifier: Expression, assumeTrue: boolean): Type {
if (!assumeTrue || operator !== SyntaxKind.EqualsEqualsToken && operator !== SyntaxKind.EqualsEqualsEqualsToken) {
return type;
}

if (!isMatchingReference(reference, constructorAccessExpr.expression)) {
return declaredType;
}

const identifierType = getTypeOfExpression(identifier);
if (!isTypeSubtypeOf(identifierType, globalFunctionType)) {
return type;
}

const prototypeProperty = getPropertyOfType(identifierType, "prototype" as __String);
if (!prototypeProperty) {
return type;
}

const prototypeType = getTypeOfSymbol(prototypeProperty);
const candidate = !isTypeAny(prototypeType) ? prototypeType : undefined;
if (!candidate || candidate === globalObjectType || candidate === globalFunctionType) {
return type;
}

return getNarrowedType(type, candidate, assumeTrue, isConstructedBy);

function isConstructedBy(source: Type, target: Type) {
if (source.flags & TypeFlags.Primitive) {
return areTypesComparable(source, target);
}

return isTypeDerivedFrom(source, target);
}
}

function narrowTypeByInstanceof(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type {
const left = getReferenceCandidate(expr.left);
if (!isMatchingReference(reference, left)) {
Expand Down
170 changes: 170 additions & 0 deletions tests/baselines/reference/typeGuardConstructor.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
tests/cases/compiler/typeGuardConstructor.ts(33,9): error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
Property 'prop1' does not exist on type 'number'.
tests/cases/compiler/typeGuardConstructor.ts(36,9): error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
Property 'prop1' does not exist on type 'number'.
tests/cases/compiler/typeGuardConstructor.ts(39,9): error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
Property 'prop1' does not exist on type 'number'.
tests/cases/compiler/typeGuardConstructor.ts(42,9): error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
Property 'prop1' does not exist on type 'number'.
tests/cases/compiler/typeGuardConstructor.ts(45,9): error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
Property 'prop1' does not exist on type 'number'.
tests/cases/compiler/typeGuardConstructor.ts(48,9): error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
Property 'prop1' does not exist on type 'number'.
tests/cases/compiler/typeGuardConstructor.ts(51,9): error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
Property 'prop1' does not exist on type 'number'.
tests/cases/compiler/typeGuardConstructor.ts(54,9): error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
Property 'prop1' does not exist on type 'number'.


==== tests/cases/compiler/typeGuardConstructor.ts (8 errors) ====
// Typical case
class Foo {
prop1: string;
}

let foo: Foo | number;
if (foo.constructor == Foo) {
foo.prop1; // string
}
if (foo["constructor"] == Foo) {
foo.prop1; // string
}
if (foo.constructor === Foo) {
foo.prop1; // string
}
if (foo["constructor"] === Foo) {
foo.prop1; // string
}
if (Foo == foo.constructor) {
foo.prop1; // string
}
if (Foo == foo["constructor"]) {
foo.prop1; // string
}
if (Foo === foo.constructor) {
foo.prop1; // string
}
if (Foo === foo["constructor"]) {
foo.prop1; // string
}

if (foo.constructor != Foo) {
foo.prop1; // ERROR
~~~~~
!!! error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
!!! error TS2339: Property 'prop1' does not exist on type 'number'.
}
if (foo["constructor"] != Foo) {
foo.prop1; // ERROR
~~~~~
!!! error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
!!! error TS2339: Property 'prop1' does not exist on type 'number'.
}
if (foo.constructor !== Foo) {
foo.prop1; // ERROR
~~~~~
!!! error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
!!! error TS2339: Property 'prop1' does not exist on type 'number'.
}
if (foo["constructor"] !== Foo) {
foo.prop1; // ERROR
~~~~~
!!! error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
!!! error TS2339: Property 'prop1' does not exist on type 'number'.
}
if (Foo != foo.constructor) {
foo.prop1; // ERROR
~~~~~
!!! error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
!!! error TS2339: Property 'prop1' does not exist on type 'number'.
}
if (Foo != foo["constructor"]) {
foo.prop1; // ERROR
~~~~~
!!! error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
!!! error TS2339: Property 'prop1' does not exist on type 'number'.
}
if (Foo !== foo.constructor) {
foo.prop1; // ERROR
~~~~~
!!! error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
!!! error TS2339: Property 'prop1' does not exist on type 'number'.
}
if (Foo !== foo["constructor"]) {
foo.prop1; // ERROR
~~~~~
!!! error TS2339: Property 'prop1' does not exist on type 'number | Foo'.
!!! error TS2339: Property 'prop1' does not exist on type 'number'.
}


// Derived class case
class Bar extends Foo {
prop2: number;
}

let bar: Bar | boolean;
if (bar.constructor === Bar) {
bar.prop1; // string
bar.prop2; // number
}
if (bar.constructor === Foo) {
bar.prop1; // string
bar.prop2; // number
}


// Union of primitives, number, arrays, and Foo
var x: number | "hello" | "world" | true | false | number[] | string[] | Foo;

if (x.constructor === Number) {
x; // number
}

if (x.constructor === String) {
x; // "hello" | "world"
}

if (x.constructor === Boolean) {
x; // boolean
}

if (x.constructor === Array) {
x; // number[] | string[]
}

if (x.constructor === Function) {
x; // declaredType
}

if (x.constructor === Foo) {
x; // Foo
x.prop1; // string
}


// Narrowing any
let a: any;

if (a.constructor === Foo) {
a; // Foo
}
if (a.constructor === "hello") {
a; // any
}
if (a.constructor === Function) {
a; // any
}


// If for some reason someone defines a type with it's own constructor property
type S = {
constructor: () => void;
};

let s: S | string;

if (s.constructor === String) {
s; // string
}

Loading