Skip to content

Commit b8648fa

Browse files
add tests for non primitive type
1 parent ce4c95f commit b8648fa

22 files changed

+404
-4
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [assignObjectToNonPrimitive.ts]
2+
var x = {};
3+
var y = {foo: "bar"};
4+
var a: object;
5+
a = x;
6+
a = y;
7+
8+
9+
//// [assignObjectToNonPrimitive.js]
10+
var x = {};
11+
var y = { foo: "bar" };
12+
var a;
13+
a = x;
14+
a = y;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/conformance/types/nonPrimitive/assignObjectToNonPrimitive.ts ===
2+
var x = {};
3+
>x : Symbol(x, Decl(assignObjectToNonPrimitive.ts, 0, 3))
4+
5+
var y = {foo: "bar"};
6+
>y : Symbol(y, Decl(assignObjectToNonPrimitive.ts, 1, 3))
7+
>foo : Symbol(foo, Decl(assignObjectToNonPrimitive.ts, 1, 9))
8+
9+
var a: object;
10+
>a : Symbol(a, Decl(assignObjectToNonPrimitive.ts, 2, 3))
11+
12+
a = x;
13+
>a : Symbol(a, Decl(assignObjectToNonPrimitive.ts, 2, 3))
14+
>x : Symbol(x, Decl(assignObjectToNonPrimitive.ts, 0, 3))
15+
16+
a = y;
17+
>a : Symbol(a, Decl(assignObjectToNonPrimitive.ts, 2, 3))
18+
>y : Symbol(y, Decl(assignObjectToNonPrimitive.ts, 1, 3))
19+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/conformance/types/nonPrimitive/assignObjectToNonPrimitive.ts ===
2+
var x = {};
3+
>x : {}
4+
>{} : {}
5+
6+
var y = {foo: "bar"};
7+
>y : { foo: string; }
8+
>{foo: "bar"} : { foo: string; }
9+
>foo : string
10+
>"bar" : "bar"
11+
12+
var a: object;
13+
>a : object
14+
15+
a = x;
16+
>a = x : {}
17+
>a : object
18+
>x : {}
19+
20+
a = y;
21+
>a = y : { foo: string; }
22+
>a : object
23+
>y : { foo: string; }
24+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
tests/cases/conformance/types/nonPrimitive/nonPriimitiveInFunction.ts(12,12): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'object'.
2+
tests/cases/conformance/types/nonPrimitive/nonPriimitiveInFunction.ts(13,1): error TS2322: Type 'object' is not assignable to type 'boolean'.
3+
tests/cases/conformance/types/nonPrimitive/nonPriimitiveInFunction.ts(17,12): error TS2322: Type 'number' is not assignable to type 'object'.
4+
5+
6+
==== tests/cases/conformance/types/nonPrimitive/nonPriimitiveInFunction.ts (3 errors) ====
7+
function takeObject(o: object) {}
8+
function returnObject(): object {
9+
return {};
10+
}
11+
12+
var nonPrimitive: object;
13+
var primitive: boolean;
14+
15+
takeObject(nonPrimitive);
16+
nonPrimitive = returnObject();
17+
18+
takeObject(primitive); // expect error
19+
~~~~~~~~~
20+
!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'object'.
21+
primitive = returnObject(); // expect error
22+
~~~~~~~~~
23+
!!! error TS2322: Type 'object' is not assignable to type 'boolean'.
24+
25+
function returnError(): object {
26+
var ret = 123;
27+
return ret; // expect error
28+
~~~
29+
!!! error TS2322: Type 'number' is not assignable to type 'object'.
30+
}
31+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//// [nonPriimitiveInFunction.ts]
2+
function takeObject(o: object) {}
3+
function returnObject(): object {
4+
return {};
5+
}
6+
7+
var nonPrimitive: object;
8+
var primitive: boolean;
9+
10+
takeObject(nonPrimitive);
11+
nonPrimitive = returnObject();
12+
13+
takeObject(primitive); // expect error
14+
primitive = returnObject(); // expect error
15+
16+
function returnError(): object {
17+
var ret = 123;
18+
return ret; // expect error
19+
}
20+
21+
22+
//// [nonPriimitiveInFunction.js]
23+
function takeObject(o) { }
24+
function returnObject() {
25+
return {};
26+
}
27+
var nonPrimitive;
28+
var primitive;
29+
takeObject(nonPrimitive);
30+
nonPrimitive = returnObject();
31+
takeObject(primitive); // expect error
32+
primitive = returnObject(); // expect error
33+
function returnError() {
34+
var ret = 123;
35+
return ret; // expect error
36+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAsProperty.ts(7,5): error TS2322: Type '{ foo: string; }' is not assignable to type 'WithNonPrimitive'.
2+
Types of property 'foo' are incompatible.
3+
Type 'string' is not assignable to type 'object'.
4+
5+
6+
==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveAsProperty.ts (1 errors) ====
7+
interface WithNonPrimitive {
8+
foo: object
9+
}
10+
11+
var a: WithNonPrimitive = { foo: {bar: "bar"} };
12+
13+
var b: WithNonPrimitive = {foo: "bar"}; // expect error
14+
~
15+
!!! error TS2322: Type '{ foo: string; }' is not assignable to type 'WithNonPrimitive'.
16+
!!! error TS2322: Types of property 'foo' are incompatible.
17+
!!! error TS2322: Type 'string' is not assignable to type 'object'.
18+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [nonPrimitiveAsProperty.ts]
2+
interface WithNonPrimitive {
3+
foo: object
4+
}
5+
6+
var a: WithNonPrimitive = { foo: {bar: "bar"} };
7+
8+
var b: WithNonPrimitive = {foo: "bar"}; // expect error
9+
10+
11+
//// [nonPrimitiveAsProperty.js]
12+
var a = { foo: { bar: "bar" } };
13+
var b = { foo: "bar" }; // expect error
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(5,1): error TS2322: Type 'object' is not assignable to type '{ foo: string; }'.
2+
Property 'foo' is missing in type 'object'.
3+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(11,1): error TS2322: Type 'number' is not assignable to type 'object'.
4+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(12,1): error TS2322: Type 'true' is not assignable to type 'object'.
5+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(13,1): error TS2322: Type 'string' is not assignable to type 'object'.
6+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(15,1): error TS2322: Type 'object' is not assignable to type 'number'.
7+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(16,1): error TS2322: Type 'object' is not assignable to type 'boolean'.
8+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(17,1): error TS2322: Type 'object' is not assignable to type 'string'.
9+
10+
11+
==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts (7 errors) ====
12+
var x = {};
13+
var y = {foo: "bar"};
14+
var a: object;
15+
x = a;
16+
y = a; // expect error
17+
~
18+
!!! error TS2322: Type 'object' is not assignable to type '{ foo: string; }'.
19+
!!! error TS2322: Property 'foo' is missing in type 'object'.
20+
21+
var n = 123;
22+
var b = true;
23+
var s = "fooo";
24+
25+
a = n; // expect error
26+
~
27+
!!! error TS2322: Type 'number' is not assignable to type 'object'.
28+
a = b; // expect error
29+
~
30+
!!! error TS2322: Type 'true' is not assignable to type 'object'.
31+
a = s; // expect error
32+
~
33+
!!! error TS2322: Type 'string' is not assignable to type 'object'.
34+
35+
n = a; // expect error
36+
~
37+
!!! error TS2322: Type 'object' is not assignable to type 'number'.
38+
b = a; // expect error
39+
~
40+
!!! error TS2322: Type 'object' is not assignable to type 'boolean'.
41+
s = a; // expect error
42+
~
43+
!!! error TS2322: Type 'object' is not assignable to type 'string'.
44+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [nonPrimitiveAssignError.ts]
2+
var x = {};
3+
var y = {foo: "bar"};
4+
var a: object;
5+
x = a;
6+
y = a; // expect error
7+
8+
var n = 123;
9+
var b = true;
10+
var s = "fooo";
11+
12+
a = n; // expect error
13+
a = b; // expect error
14+
a = s; // expect error
15+
16+
n = a; // expect error
17+
b = a; // expect error
18+
s = a; // expect error
19+
20+
21+
//// [nonPrimitiveAssignError.js]
22+
var x = {};
23+
var y = { foo: "bar" };
24+
var a;
25+
x = a;
26+
y = a; // expect error
27+
var n = 123;
28+
var b = true;
29+
var s = "fooo";
30+
a = n; // expect error
31+
a = b; // expect error
32+
a = s; // expect error
33+
n = a; // expect error
34+
b = a; // expect error
35+
s = a; // expect error
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveInGeneric.ts(7,17): error TS2345: Argument of type '123' is not assignable to parameter of type 'object'.
2+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveInGeneric.ts(8,17): error TS2345: Argument of type 'string' is not assignable to parameter of type 'object'.
3+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveInGeneric.ts(14,7): error TS2345: Argument of type '123' is not assignable to parameter of type 'object'.
4+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveInGeneric.ts(15,7): error TS2345: Argument of type 'string' is not assignable to parameter of type 'object'.
5+
6+
7+
==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveInGeneric.ts (4 errors) ====
8+
function generic<T>(t: T) {}
9+
var a = {};
10+
var b = "42";
11+
12+
generic<object>({});
13+
generic<object>(a);
14+
generic<object>(123); // expect error
15+
~~~
16+
!!! error TS2345: Argument of type '123' is not assignable to parameter of type 'object'.
17+
generic<object>(b); // expect error
18+
~
19+
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'object'.
20+
21+
function bound<T extends object>(t: T) {}
22+
23+
bound({});
24+
bound(a);
25+
bound(123); // expect error
26+
~~~
27+
!!! error TS2345: Argument of type '123' is not assignable to parameter of type 'object'.
28+
bound(b); // expect error
29+
~
30+
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'object'.
31+

0 commit comments

Comments
 (0)