Skip to content
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

add type guard of constructor #23622

Closed
wants to merge 5 commits 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
36 changes: 34 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14722,6 +14722,18 @@ namespace ts {
if (containsMatchingReferenceDiscriminant(reference, left) || containsMatchingReferenceDiscriminant(reference, right)) {
return declaredType;
}
if (isPropertyAccessExpression(left) && idText(left.name) === "constructor") {
return narrowTypeByConstructor(type, expr.right, operator, assumeTrue);
}
if (isPropertyAccessExpression(right) && idText(right.name) === "constructor") {
return narrowTypeByConstructor(type, expr.left, operator, assumeTrue);
}
if (isElementAccessExpression(left) && isStringLiteralLike(left.argumentExpression) && left.argumentExpression.text === "constructor") {
return narrowTypeByConstructor(type, expr.right, operator, assumeTrue);
}
if (isElementAccessExpression(right) && isStringLiteralLike(right.argumentExpression) && right.argumentExpression.text === "constructor") {
return narrowTypeByConstructor(type, expr.left, operator, assumeTrue);
}
break;
case SyntaxKind.InstanceOfKeyword:
return narrowTypeByInstanceof(type, expr, assumeTrue);
Expand All @@ -14741,7 +14753,7 @@ namespace ts {
if (type.flags & TypeFlags.Any) {
return type;
}
if (operator === SyntaxKind.ExclamationEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken) {
if (isNegatedEqualityToken(operator)) {
assumeTrue = !assumeTrue;
}
const valueType = getTypeOfExpression(value);
Expand Down Expand Up @@ -14771,6 +14783,26 @@ namespace ts {
return type;
}

function narrowTypeByConstructor(type: Type, expr: Expression, operator: SyntaxKind, assumeTrue: boolean): Type {
if (!assumeTrue || isNegatedEqualityToken(operator)) return type;

const rightType = getTypeOfExpression(expr);
if (!isTypeSubtypeOf(rightType, globalFunctionType)) return type;

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

const prototypePropertyType = getTypeOfSymbol(prototypeProperty);
const targetType = !isTypeAny(prototypePropertyType) ? prototypePropertyType : undefined;
if (!targetType || isTypeAny(type) && (targetType === globalObjectType || targetType === globalFunctionType)) return type;

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

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

function narrowTypeByTypeof(type: Type, typeOfExpr: TypeOfExpression, operator: SyntaxKind, literal: LiteralExpression, assumeTrue: boolean): Type {
// We have '==', '!=', '====', or !==' operator with 'typeof xxx' and string literal operands
const target = getReferenceCandidate(typeOfExpr.expression);
Expand All @@ -14782,7 +14814,7 @@ namespace ts {
}
return type;
}
if (operator === SyntaxKind.ExclamationEqualsToken || operator === SyntaxKind.ExclamationEqualsEqualsToken) {
if (isNegatedEqualityToken(operator)) {
assumeTrue = !assumeTrue;
}
if (type.flags & TypeFlags.Any && literal.text === "function") {
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6685,6 +6685,11 @@ namespace ts {
return node.kind === SyntaxKind.NamedImports || node.kind === SyntaxKind.NamedExports;
}

/** @internal */
export function isNegatedEqualityToken(kind: SyntaxKind): boolean {
return kind === SyntaxKind.ExclamationEqualsToken || kind === SyntaxKind.ExclamationEqualsEqualsToken;
}

export interface ObjectAllocator {
getNodeConstructor(): new (kind: SyntaxKind, pos?: number, end?: number) => Node;
getTokenConstructor(): new <TKind extends SyntaxKind>(kind: TKind, pos?: number, end?: number) => Token<TKind>;
Expand Down
117 changes: 117 additions & 0 deletions tests/baselines/reference/typeGuardConstructor.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
tests/cases/conformance/expressions/typeGuards/typeGuardConstructor.ts(45,11): error TS2339: Property 'p2' does not exist on type 'C1 | C2'.
Property 'p2' does not exist on type 'C1'.
tests/cases/conformance/expressions/typeGuards/typeGuardConstructor.ts(49,11): error TS2339: Property 'p2' does not exist on type 'C1 | C2'.
Property 'p2' does not exist on type 'C1'.
tests/cases/conformance/expressions/typeGuards/typeGuardConstructor.ts(52,11): error TS2339: Property 'p1' does not exist on type 'C1 | C2'.
Property 'p1' does not exist on type 'C2'.
tests/cases/conformance/expressions/typeGuards/typeGuardConstructor.ts(63,11): error TS2339: Property 'p4' does not exist on type 'C1 | C2 | C3'.
Property 'p4' does not exist on type 'C1'.


==== tests/cases/conformance/expressions/typeGuards/typeGuardConstructor.ts (4 errors) ====
class C1 {
p1: string;
}
class C2 {
p2: number;
}
class D1 extends C1 {
p3: number;
}
class C3 {
p4: number;
}
class D2 extends D1 {
p5: number
}

var a: C1;
if (a.constructor === D1) {
a.p3;
}
if (a.constructor == D1) {
a.p3;
}
if (D1 === a.constructor) {
a.p3;
}
if (a["constructor"] === D1) {
a.p3;
}
if (D1 === a["constructor"]) {
a.p3;
}

var b: C1;
if (b.constructor === D2) {
b.p3;
b.p5;
}

var ctor3: C1 | C2;
if (ctor3.constructor === C1) {
ctor3.p1; // C1
}
else {
ctor3.p2; // C2
~~
!!! error TS2339: Property 'p2' does not exist on type 'C1 | C2'.
!!! error TS2339: Property 'p2' does not exist on type 'C1'.
}

if (ctor3.constructor !== C1) {
ctor3.p2; // C1
~~
!!! error TS2339: Property 'p2' does not exist on type 'C1 | C2'.
!!! error TS2339: Property 'p2' does not exist on type 'C1'.
}
else {
ctor3.p1; // C2
~~
!!! error TS2339: Property 'p1' does not exist on type 'C1 | C2'.
!!! error TS2339: Property 'p1' does not exist on type 'C2'.
}

var ctor4: C1 | C2 | C3;
if (ctor4.constructor === C1) {
ctor4.p1; // C1
}
else if (ctor4.constructor === C2) {
ctor4.p2; // C2
}
else {
ctor4.p4; // C3
~~
!!! error TS2339: Property 'p4' does not exist on type 'C1 | C2 | C3'.
!!! error TS2339: Property 'p4' does not exist on type 'C1'.
}

var x: number | "hello" | "world" | true | 1[] | undefined;
if (x.constructor === String) {
x.length;
}

if (x.constructor === Number) {
x.toFixed();
}

if (x.constructor === Boolean) {
const b = x;
}

if(x.constructor === Array) {
const c = x[0];
}


class Bar {
a: string
}

class Baz {
a: string
}
var bar: Bar | Baz;
if (bar.constructor === Baz) {
const baz = bar
}
Loading