Skip to content

Commit a99c37e

Browse files
committed
fix type compare
1 parent 93cd249 commit a99c37e

File tree

6 files changed

+117
-3
lines changed

6 files changed

+117
-3
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13548,7 +13548,11 @@ namespace ts {
1354813548
const targetType = !isTypeAny(prototypePropertyType) ? prototypePropertyType : undefined;
1354913549
if (!targetType || isTypeAny(type) && (targetType === globalObjectType || targetType === globalFunctionType)) return type;
1355013550

13551-
return getNarrowedType(type, targetType, assumeTrue, areTypesComparable);
13551+
return getNarrowedType(type, targetType, assumeTrue, isConstructedBy);
13552+
13553+
function isConstructedBy(source: Type, target: Type) {
13554+
return source.flags & TypeFlags.Primitive ? areTypesComparable(source, target) : isTypeDerivedFrom(source, target);
13555+
}
1355213556
}
1355313557

1355413558
function narrowTypeByTypeof(type: Type, typeOfExpr: TypeOfExpression, operator: SyntaxKind, literal: LiteralExpression, assumeTrue: boolean): Type {

tests/baselines/reference/typeGuardConstructor.errors.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,16 @@ tests/cases/conformance/expressions/typeGuards/typeGuardConstructor.ts(63,11): e
102102
if(x.constructor === Array) {
103103
const c = x[0];
104104
}
105-
105+
106+
107+
class Bar {
108+
a: string
109+
}
110+
111+
class Baz {
112+
a: string
113+
}
114+
var bar: Bar | Baz;
115+
if (bar.constructor === Baz) {
116+
const baz = bar
117+
}

tests/baselines/reference/typeGuardConstructor.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,19 @@ if (x.constructor === Boolean) {
8080
if(x.constructor === Array) {
8181
const c = x[0];
8282
}
83-
83+
84+
85+
class Bar {
86+
a: string
87+
}
88+
89+
class Baz {
90+
a: string
91+
}
92+
var bar: Bar | Baz;
93+
if (bar.constructor === Baz) {
94+
const baz = bar
95+
}
8496

8597
//// [typeGuardConstructor.js]
8698
var __extends = (this && this.__extends) || (function () {
@@ -179,3 +191,17 @@ if (x.constructor === Boolean) {
179191
if (x.constructor === Array) {
180192
var c = x[0];
181193
}
194+
var Bar = /** @class */ (function () {
195+
function Bar() {
196+
}
197+
return Bar;
198+
}());
199+
var Baz = /** @class */ (function () {
200+
function Baz() {
201+
}
202+
return Baz;
203+
}());
204+
var bar;
205+
if (bar.constructor === Baz) {
206+
var baz = bar;
207+
}

tests/baselines/reference/typeGuardConstructor.symbols

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,3 +228,32 @@ if(x.constructor === Array) {
228228
>x : Symbol(x, Decl(typeGuardConstructor.ts, 65, 3))
229229
}
230230

231+
232+
class Bar {
233+
>Bar : Symbol(Bar, Decl(typeGuardConstructor.ts, 80, 1))
234+
235+
a: string
236+
>a : Symbol(Bar.a, Decl(typeGuardConstructor.ts, 83, 11))
237+
}
238+
239+
class Baz {
240+
>Baz : Symbol(Baz, Decl(typeGuardConstructor.ts, 85, 1))
241+
242+
a: string
243+
>a : Symbol(Baz.a, Decl(typeGuardConstructor.ts, 87, 11))
244+
}
245+
var bar: Bar | Baz;
246+
>bar : Symbol(bar, Decl(typeGuardConstructor.ts, 90, 3))
247+
>Bar : Symbol(Bar, Decl(typeGuardConstructor.ts, 80, 1))
248+
>Baz : Symbol(Baz, Decl(typeGuardConstructor.ts, 85, 1))
249+
250+
if (bar.constructor === Baz) {
251+
>bar.constructor : Symbol(Object.constructor, Decl(lib.d.ts, --, --))
252+
>bar : Symbol(bar, Decl(typeGuardConstructor.ts, 90, 3))
253+
>constructor : Symbol(Object.constructor, Decl(lib.d.ts, --, --))
254+
>Baz : Symbol(Baz, Decl(typeGuardConstructor.ts, 85, 1))
255+
256+
const baz = bar
257+
>baz : Symbol(baz, Decl(typeGuardConstructor.ts, 92, 9))
258+
>bar : Symbol(bar, Decl(typeGuardConstructor.ts, 90, 3))
259+
}

tests/baselines/reference/typeGuardConstructor.types

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,33 @@ if(x.constructor === Array) {
256256
>0 : 0
257257
}
258258

259+
260+
class Bar {
261+
>Bar : Bar
262+
263+
a: string
264+
>a : string
265+
}
266+
267+
class Baz {
268+
>Baz : Baz
269+
270+
a: string
271+
>a : string
272+
}
273+
var bar: Bar | Baz;
274+
>bar : Bar | Baz
275+
>Bar : Bar
276+
>Baz : Baz
277+
278+
if (bar.constructor === Baz) {
279+
>bar.constructor === Baz : boolean
280+
>bar.constructor : Function
281+
>bar : Bar | Baz
282+
>constructor : Function
283+
>Baz : typeof Baz
284+
285+
const baz = bar
286+
>baz : Baz
287+
>bar : Baz
288+
}

tests/cases/conformance/expressions/typeGuards/typeGuardConstructor.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,16 @@ if (x.constructor === Boolean) {
7979
if(x.constructor === Array) {
8080
const c = x[0];
8181
}
82+
83+
84+
class Bar {
85+
a: string
86+
}
87+
88+
class Baz {
89+
a: string
90+
}
91+
var bar: Bar | Baz;
92+
if (bar.constructor === Baz) {
93+
const baz = bar
94+
}

0 commit comments

Comments
 (0)