Skip to content

Commit 26eb6ab

Browse files
authored
Primitives should not be instanceof... anything (#27402)
1 parent 9cf201c commit 26eb6ab

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10595,6 +10595,7 @@ namespace ts {
1059510595
function isTypeDerivedFrom(source: Type, target: Type): boolean {
1059610596
return source.flags & TypeFlags.Union ? every((<UnionType>source).types, t => isTypeDerivedFrom(t, target)) :
1059710597
target.flags & TypeFlags.Union ? some((<UnionType>target).types, t => isTypeDerivedFrom(source, t)) :
10598+
source.flags & TypeFlags.Primitive && !(target.flags & TypeFlags.Primitive) ? false :
1059810599
source.flags & TypeFlags.InstantiableNonPrimitive ? isTypeDerivedFrom(getBaseConstraintOfType(source) || emptyObjectType, target) :
1059910600
target === globalObjectType || target === globalFunctionType ? isTypeSubtypeOf(source, target) :
1060010601
hasBaseType(source, getTargetType(target));
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [controlFlowInstanceOfGuardPrimitives.ts]
2+
function distinguish(thing: string | number | Date) {
3+
if (thing instanceof Object) {
4+
console.log("Aha!! It's a Date in " + thing.getFullYear());
5+
} else if (typeof thing === 'string') {
6+
console.log("Aha!! It's a string of length " + thing.length);
7+
} else {
8+
console.log("Aha!! It's the number " + thing.toPrecision(3));
9+
}
10+
}
11+
12+
distinguish(new Date());
13+
distinguish("beef");
14+
distinguish(3.14159265);
15+
16+
//// [controlFlowInstanceOfGuardPrimitives.js]
17+
function distinguish(thing) {
18+
if (thing instanceof Object) {
19+
console.log("Aha!! It's a Date in " + thing.getFullYear());
20+
}
21+
else if (typeof thing === 'string') {
22+
console.log("Aha!! It's a string of length " + thing.length);
23+
}
24+
else {
25+
console.log("Aha!! It's the number " + thing.toPrecision(3));
26+
}
27+
}
28+
distinguish(new Date());
29+
distinguish("beef");
30+
distinguish(3.14159265);
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
=== tests/cases/conformance/controlFlow/controlFlowInstanceOfGuardPrimitives.ts ===
2+
function distinguish(thing: string | number | Date) {
3+
>distinguish : Symbol(distinguish, Decl(controlFlowInstanceOfGuardPrimitives.ts, 0, 0))
4+
>thing : Symbol(thing, Decl(controlFlowInstanceOfGuardPrimitives.ts, 0, 21))
5+
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))
6+
7+
if (thing instanceof Object) {
8+
>thing : Symbol(thing, Decl(controlFlowInstanceOfGuardPrimitives.ts, 0, 21))
9+
>Object : Symbol(Object, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
10+
11+
console.log("Aha!! It's a Date in " + thing.getFullYear());
12+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
13+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
14+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
15+
>thing.getFullYear : Symbol(Date.getFullYear, Decl(lib.es5.d.ts, --, --))
16+
>thing : Symbol(thing, Decl(controlFlowInstanceOfGuardPrimitives.ts, 0, 21))
17+
>getFullYear : Symbol(Date.getFullYear, Decl(lib.es5.d.ts, --, --))
18+
19+
} else if (typeof thing === 'string') {
20+
>thing : Symbol(thing, Decl(controlFlowInstanceOfGuardPrimitives.ts, 0, 21))
21+
22+
console.log("Aha!! It's a string of length " + thing.length);
23+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
24+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
25+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
26+
>thing.length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
27+
>thing : Symbol(thing, Decl(controlFlowInstanceOfGuardPrimitives.ts, 0, 21))
28+
>length : Symbol(String.length, Decl(lib.es5.d.ts, --, --))
29+
30+
} else {
31+
console.log("Aha!! It's the number " + thing.toPrecision(3));
32+
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
33+
>console : Symbol(console, Decl(lib.dom.d.ts, --, --))
34+
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --))
35+
>thing.toPrecision : Symbol(Number.toPrecision, Decl(lib.es5.d.ts, --, --))
36+
>thing : Symbol(thing, Decl(controlFlowInstanceOfGuardPrimitives.ts, 0, 21))
37+
>toPrecision : Symbol(Number.toPrecision, Decl(lib.es5.d.ts, --, --))
38+
}
39+
}
40+
41+
distinguish(new Date());
42+
>distinguish : Symbol(distinguish, Decl(controlFlowInstanceOfGuardPrimitives.ts, 0, 0))
43+
>Date : Symbol(Date, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --), Decl(lib.scripthost.d.ts, --, --))
44+
45+
distinguish("beef");
46+
>distinguish : Symbol(distinguish, Decl(controlFlowInstanceOfGuardPrimitives.ts, 0, 0))
47+
48+
distinguish(3.14159265);
49+
>distinguish : Symbol(distinguish, Decl(controlFlowInstanceOfGuardPrimitives.ts, 0, 0))
50+
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
=== tests/cases/conformance/controlFlow/controlFlowInstanceOfGuardPrimitives.ts ===
2+
function distinguish(thing: string | number | Date) {
3+
>distinguish : (thing: string | number | Date) => void
4+
>thing : string | number | Date
5+
6+
if (thing instanceof Object) {
7+
>thing instanceof Object : boolean
8+
>thing : string | number | Date
9+
>Object : ObjectConstructor
10+
11+
console.log("Aha!! It's a Date in " + thing.getFullYear());
12+
>console.log("Aha!! It's a Date in " + thing.getFullYear()) : void
13+
>console.log : (message?: any, ...optionalParams: any[]) => void
14+
>console : Console
15+
>log : (message?: any, ...optionalParams: any[]) => void
16+
>"Aha!! It's a Date in " + thing.getFullYear() : string
17+
>"Aha!! It's a Date in " : "Aha!! It's a Date in "
18+
>thing.getFullYear() : number
19+
>thing.getFullYear : () => number
20+
>thing : Date
21+
>getFullYear : () => number
22+
23+
} else if (typeof thing === 'string') {
24+
>typeof thing === 'string' : boolean
25+
>typeof thing : "string" | "number" | "boolean" | "symbol" | "undefined" | "object" | "function"
26+
>thing : string | number
27+
>'string' : "string"
28+
29+
console.log("Aha!! It's a string of length " + thing.length);
30+
>console.log("Aha!! It's a string of length " + thing.length) : void
31+
>console.log : (message?: any, ...optionalParams: any[]) => void
32+
>console : Console
33+
>log : (message?: any, ...optionalParams: any[]) => void
34+
>"Aha!! It's a string of length " + thing.length : string
35+
>"Aha!! It's a string of length " : "Aha!! It's a string of length "
36+
>thing.length : number
37+
>thing : string
38+
>length : number
39+
40+
} else {
41+
console.log("Aha!! It's the number " + thing.toPrecision(3));
42+
>console.log("Aha!! It's the number " + thing.toPrecision(3)) : void
43+
>console.log : (message?: any, ...optionalParams: any[]) => void
44+
>console : Console
45+
>log : (message?: any, ...optionalParams: any[]) => void
46+
>"Aha!! It's the number " + thing.toPrecision(3) : string
47+
>"Aha!! It's the number " : "Aha!! It's the number "
48+
>thing.toPrecision(3) : string
49+
>thing.toPrecision : (precision?: number) => string
50+
>thing : number
51+
>toPrecision : (precision?: number) => string
52+
>3 : 3
53+
}
54+
}
55+
56+
distinguish(new Date());
57+
>distinguish(new Date()) : void
58+
>distinguish : (thing: string | number | Date) => void
59+
>new Date() : Date
60+
>Date : DateConstructor
61+
62+
distinguish("beef");
63+
>distinguish("beef") : void
64+
>distinguish : (thing: string | number | Date) => void
65+
>"beef" : "beef"
66+
67+
distinguish(3.14159265);
68+
>distinguish(3.14159265) : void
69+
>distinguish : (thing: string | number | Date) => void
70+
>3.14159265 : 3.14159265
71+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function distinguish(thing: string | number | Date) {
2+
if (thing instanceof Object) {
3+
console.log("Aha!! It's a Date in " + thing.getFullYear());
4+
} else if (typeof thing === 'string') {
5+
console.log("Aha!! It's a string of length " + thing.length);
6+
} else {
7+
console.log("Aha!! It's the number " + thing.toPrecision(3));
8+
}
9+
}
10+
11+
distinguish(new Date());
12+
distinguish("beef");
13+
distinguish(3.14159265);

0 commit comments

Comments
 (0)