Skip to content

Commit 2fb51e7

Browse files
address code review feedback
1 parent b8648fa commit 2fb51e7

File tree

6 files changed

+89
-5
lines changed

6 files changed

+89
-5
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3089,10 +3089,6 @@ namespace ts {
30893089
return type && (type.flags & TypeFlags.Never) !== 0;
30903090
}
30913091

3092-
function isTypeNonPrimitive(type: Type) {
3093-
return type === nonPrimitiveType;
3094-
}
3095-
30963092
// Return the type of a binding element parent. We check SymbolLinks first to see if a type has been
30973093
// assigned by contextual typing.
30983094
function getTypeForBindingElementParent(node: VariableLikeDeclaration) {
@@ -7475,7 +7471,7 @@ namespace ts {
74757471
}
74767472
}
74777473
}
7478-
else if (!(source.flags & TypeFlags.Primitive && isTypeNonPrimitive(target))) {
7474+
else if (!(source.flags & TypeFlags.Primitive && target === nonPrimitiveType)) {
74797475
if (getObjectFlags(source) & ObjectFlags.Reference && getObjectFlags(target) & ObjectFlags.Reference && (<TypeReference>source).target === (<TypeReference>target).target) {
74807476
// We have type references to same target type, see if relationship holds for all type arguments
74817477
if (result = typeArgumentsRelatedTo(<TypeReference>source, <TypeReference>target, reportErrors)) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var a: object;
2+
a.toString();
3+
a.nonExist(); // error

tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,11 @@ a = s; // expect error
1515
n = a; // expect error
1616
b = a; // expect error
1717
s = a; // expect error
18+
19+
var numObj: Number = 123;
20+
var boolObj: Boolean = true;
21+
var strObj: String = "string";
22+
23+
a = numObj; // ok
24+
a = boolObj; // ok
25+
a = strObj; // ok

tests/cases/conformance/types/nonPrimitive/nonPrimitiveInGeneric.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,10 @@ bound({});
1313
bound(a);
1414
bound(123); // expect error
1515
bound(b); // expect error
16+
17+
function bound2<T extends object>() {}
18+
19+
bound2<{}>();
20+
bound2<Object>();
21+
bound2<number>(); // expect error
22+
bound2<string>(); // expect error
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Narrow {
2+
narrowed: boolean
3+
}
4+
5+
var a: object
6+
7+
if (a instanceof Narrow) {
8+
a.narrowed; // ok
9+
a = 123; // error
10+
}
11+
12+
if (typeof a === 'number') {
13+
a.toFixed(); // error, never
14+
}
15+
16+
var b: object | null
17+
18+
if (typeof b === 'object') {
19+
b.toString(); // error, object | null
20+
} else {
21+
b.toString(); // error, never
22+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// @strictNullChecks: true
2+
3+
var a: object
4+
declare var b: object | null
5+
declare var c: object | undefined
6+
declare var d: object | null | undefined
7+
var e: object | null
8+
a.toString; // error
9+
a = undefined; // error
10+
a = null; // error
11+
a = b; // error
12+
a = c; // error
13+
a = d; // error
14+
15+
e = a; // ok
16+
a = e; // ok
17+
18+
if (typeof b !== 'object') {
19+
b.toString(); // error, never
20+
}
21+
22+
if (typeof b === 'object') {
23+
a = b; // error, b is not narrowed
24+
}
25+
26+
if (typeof d === 'object') {
27+
b = d; // ok
28+
} else {
29+
d; // undefined
30+
}
31+
32+
if (d == null) {
33+
d; // null | undefined
34+
} else {
35+
d.toString(); // object
36+
}
37+
38+
if (d === null) {
39+
d; // null
40+
} else {
41+
d.toString(); // error, object | undefined
42+
}
43+
44+
if (typeof d === 'undefined') {
45+
d; // undefined
46+
} else {
47+
d.toString(); // error, object | null
48+
}

0 commit comments

Comments
 (0)