add tests for non primitive type

This commit is contained in:
Herrington Darkholme
2016-11-26 00:32:55 +08:00
parent ce4c95f332
commit b8648fa9f6
22 changed files with 404 additions and 4 deletions

View File

@@ -0,0 +1,14 @@
//// [assignObjectToNonPrimitive.ts]
var x = {};
var y = {foo: "bar"};
var a: object;
a = x;
a = y;
//// [assignObjectToNonPrimitive.js]
var x = {};
var y = { foo: "bar" };
var a;
a = x;
a = y;

View File

@@ -0,0 +1,19 @@
=== tests/cases/conformance/types/nonPrimitive/assignObjectToNonPrimitive.ts ===
var x = {};
>x : Symbol(x, Decl(assignObjectToNonPrimitive.ts, 0, 3))
var y = {foo: "bar"};
>y : Symbol(y, Decl(assignObjectToNonPrimitive.ts, 1, 3))
>foo : Symbol(foo, Decl(assignObjectToNonPrimitive.ts, 1, 9))
var a: object;
>a : Symbol(a, Decl(assignObjectToNonPrimitive.ts, 2, 3))
a = x;
>a : Symbol(a, Decl(assignObjectToNonPrimitive.ts, 2, 3))
>x : Symbol(x, Decl(assignObjectToNonPrimitive.ts, 0, 3))
a = y;
>a : Symbol(a, Decl(assignObjectToNonPrimitive.ts, 2, 3))
>y : Symbol(y, Decl(assignObjectToNonPrimitive.ts, 1, 3))

View File

@@ -0,0 +1,24 @@
=== tests/cases/conformance/types/nonPrimitive/assignObjectToNonPrimitive.ts ===
var x = {};
>x : {}
>{} : {}
var y = {foo: "bar"};
>y : { foo: string; }
>{foo: "bar"} : { foo: string; }
>foo : string
>"bar" : "bar"
var a: object;
>a : object
a = x;
>a = x : {}
>a : object
>x : {}
a = y;
>a = y : { foo: string; }
>a : object
>y : { foo: string; }

View File

@@ -0,0 +1,31 @@
tests/cases/conformance/types/nonPrimitive/nonPriimitiveInFunction.ts(12,12): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'object'.
tests/cases/conformance/types/nonPrimitive/nonPriimitiveInFunction.ts(13,1): error TS2322: Type 'object' is not assignable to type 'boolean'.
tests/cases/conformance/types/nonPrimitive/nonPriimitiveInFunction.ts(17,12): error TS2322: Type 'number' is not assignable to type 'object'.
==== tests/cases/conformance/types/nonPrimitive/nonPriimitiveInFunction.ts (3 errors) ====
function takeObject(o: object) {}
function returnObject(): object {
return {};
}
var nonPrimitive: object;
var primitive: boolean;
takeObject(nonPrimitive);
nonPrimitive = returnObject();
takeObject(primitive); // expect error
~~~~~~~~~
!!! error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'object'.
primitive = returnObject(); // expect error
~~~~~~~~~
!!! error TS2322: Type 'object' is not assignable to type 'boolean'.
function returnError(): object {
var ret = 123;
return ret; // expect error
~~~
!!! error TS2322: Type 'number' is not assignable to type 'object'.
}

View File

@@ -0,0 +1,36 @@
//// [nonPriimitiveInFunction.ts]
function takeObject(o: object) {}
function returnObject(): object {
return {};
}
var nonPrimitive: object;
var primitive: boolean;
takeObject(nonPrimitive);
nonPrimitive = returnObject();
takeObject(primitive); // expect error
primitive = returnObject(); // expect error
function returnError(): object {
var ret = 123;
return ret; // expect error
}
//// [nonPriimitiveInFunction.js]
function takeObject(o) { }
function returnObject() {
return {};
}
var nonPrimitive;
var primitive;
takeObject(nonPrimitive);
nonPrimitive = returnObject();
takeObject(primitive); // expect error
primitive = returnObject(); // expect error
function returnError() {
var ret = 123;
return ret; // expect error
}

View File

@@ -0,0 +1,18 @@
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAsProperty.ts(7,5): error TS2322: Type '{ foo: string; }' is not assignable to type 'WithNonPrimitive'.
Types of property 'foo' are incompatible.
Type 'string' is not assignable to type 'object'.
==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveAsProperty.ts (1 errors) ====
interface WithNonPrimitive {
foo: object
}
var a: WithNonPrimitive = { foo: {bar: "bar"} };
var b: WithNonPrimitive = {foo: "bar"}; // expect error
~
!!! error TS2322: Type '{ foo: string; }' is not assignable to type 'WithNonPrimitive'.
!!! error TS2322: Types of property 'foo' are incompatible.
!!! error TS2322: Type 'string' is not assignable to type 'object'.

View File

@@ -0,0 +1,13 @@
//// [nonPrimitiveAsProperty.ts]
interface WithNonPrimitive {
foo: object
}
var a: WithNonPrimitive = { foo: {bar: "bar"} };
var b: WithNonPrimitive = {foo: "bar"}; // expect error
//// [nonPrimitiveAsProperty.js]
var a = { foo: { bar: "bar" } };
var b = { foo: "bar" }; // expect error

View File

@@ -0,0 +1,44 @@
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(5,1): error TS2322: Type 'object' is not assignable to type '{ foo: string; }'.
Property 'foo' is missing in type 'object'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(11,1): error TS2322: Type 'number' is not assignable to type 'object'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(12,1): error TS2322: Type 'true' is not assignable to type 'object'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(13,1): error TS2322: Type 'string' is not assignable to type 'object'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(15,1): error TS2322: Type 'object' is not assignable to type 'number'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(16,1): error TS2322: Type 'object' is not assignable to type 'boolean'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts(17,1): error TS2322: Type 'object' is not assignable to type 'string'.
==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveAssignError.ts (7 errors) ====
var x = {};
var y = {foo: "bar"};
var a: object;
x = a;
y = a; // expect error
~
!!! error TS2322: Type 'object' is not assignable to type '{ foo: string; }'.
!!! error TS2322: Property 'foo' is missing in type 'object'.
var n = 123;
var b = true;
var s = "fooo";
a = n; // expect error
~
!!! error TS2322: Type 'number' is not assignable to type 'object'.
a = b; // expect error
~
!!! error TS2322: Type 'true' is not assignable to type 'object'.
a = s; // expect error
~
!!! error TS2322: Type 'string' is not assignable to type 'object'.
n = a; // expect error
~
!!! error TS2322: Type 'object' is not assignable to type 'number'.
b = a; // expect error
~
!!! error TS2322: Type 'object' is not assignable to type 'boolean'.
s = a; // expect error
~
!!! error TS2322: Type 'object' is not assignable to type 'string'.

View File

@@ -0,0 +1,35 @@
//// [nonPrimitiveAssignError.ts]
var x = {};
var y = {foo: "bar"};
var a: object;
x = a;
y = a; // expect error
var n = 123;
var b = true;
var s = "fooo";
a = n; // expect error
a = b; // expect error
a = s; // expect error
n = a; // expect error
b = a; // expect error
s = a; // expect error
//// [nonPrimitiveAssignError.js]
var x = {};
var y = { foo: "bar" };
var a;
x = a;
y = a; // expect error
var n = 123;
var b = true;
var s = "fooo";
a = n; // expect error
a = b; // expect error
a = s; // expect error
n = a; // expect error
b = a; // expect error
s = a; // expect error

View File

@@ -0,0 +1,31 @@
tests/cases/conformance/types/nonPrimitive/nonPrimitiveInGeneric.ts(7,17): error TS2345: Argument of type '123' is not assignable to parameter of type 'object'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveInGeneric.ts(8,17): error TS2345: Argument of type 'string' is not assignable to parameter of type 'object'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveInGeneric.ts(14,7): error TS2345: Argument of type '123' is not assignable to parameter of type 'object'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveInGeneric.ts(15,7): error TS2345: Argument of type 'string' is not assignable to parameter of type 'object'.
==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveInGeneric.ts (4 errors) ====
function generic<T>(t: T) {}
var a = {};
var b = "42";
generic<object>({});
generic<object>(a);
generic<object>(123); // expect error
~~~
!!! error TS2345: Argument of type '123' is not assignable to parameter of type 'object'.
generic<object>(b); // expect error
~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'object'.
function bound<T extends object>(t: T) {}
bound({});
bound(a);
bound(123); // expect error
~~~
!!! error TS2345: Argument of type '123' is not assignable to parameter of type 'object'.
bound(b); // expect error
~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'object'.

View File

@@ -0,0 +1,31 @@
//// [nonPrimitiveInGeneric.ts]
function generic<T>(t: T) {}
var a = {};
var b = "42";
generic<object>({});
generic<object>(a);
generic<object>(123); // expect error
generic<object>(b); // expect error
function bound<T extends object>(t: T) {}
bound({});
bound(a);
bound(123); // expect error
bound(b); // expect error
//// [nonPrimitiveInGeneric.js]
function generic(t) { }
var a = {};
var b = "42";
generic({});
generic(a);
generic(123); // expect error
generic(b); // expect error
function bound(t) { }
bound({});
bound(a);
bound(123); // expect error
bound(b); // expect error

View File

@@ -0,0 +1,18 @@
tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(1,5): error TS2322: Type '""' is not assignable to type 'object & string'.
Type '""' is not assignable to type 'object'.
tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts(3,1): error TS2322: Type 'string' is not assignable to type 'object & string'.
Type 'string' is not assignable to type 'object'.
==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveUnionIntersection.ts (2 errors) ====
var a: object & string = ""; // error
~
!!! error TS2322: Type '""' is not assignable to type 'object & string'.
!!! error TS2322: Type '""' is not assignable to type 'object'.
var b: object | string = ""; // ok
a = b; // error
~
!!! error TS2322: Type 'string' is not assignable to type 'object & string'.
!!! error TS2322: Type 'string' is not assignable to type 'object'.
b = a; // ok

View File

@@ -0,0 +1,12 @@
//// [nonPrimitiveUnionIntersection.ts]
var a: object & string = ""; // error
var b: object | string = ""; // ok
a = b; // error
b = a; // ok
//// [nonPrimitiveUnionIntersection.js]
var a = ""; // error
var b = ""; // ok
a = b; // error
b = a; // ok

View File

@@ -6,9 +6,10 @@ tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(6,1): error
tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(6,6): error TS1005: ';' expected.
tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(6,11): error TS1109: Expression expected.
tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(6,13): error TS2693: 'I' only refers to a type, but is being used as a value here.
tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(7,6): error TS2457: Type alias name cannot be 'object'
==== tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts (8 errors) ====
==== tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts (9 errors) ====
interface I {}
type any = I;
~~~
@@ -30,4 +31,8 @@ tests/cases/conformance/types/typeAliases/reservedNamesInAliases.ts(6,13): error
~
!!! error TS1109: Expression expected.
~
!!! error TS2693: 'I' only refers to a type, but is being used as a value here.
!!! error TS2693: 'I' only refers to a type, but is being used as a value here.
type object = I;
~~~~~~
!!! error TS2457: Type alias name cannot be 'object'

View File

@@ -4,7 +4,9 @@ type any = I;
type number = I;
type boolean = I;
type string = I;
type void = I;
type void = I;
type object = I;
//// [reservedNamesInAliases.js]
type;

View File

@@ -0,0 +1,5 @@
var x = {};
var y = {foo: "bar"};
var a: object;
a = x;
a = y;

View File

@@ -0,0 +1,18 @@
function takeObject(o: object) {}
function returnObject(): object {
return {};
}
var nonPrimitive: object;
var primitive: boolean;
takeObject(nonPrimitive);
nonPrimitive = returnObject();
takeObject(primitive); // expect error
primitive = returnObject(); // expect error
function returnError(): object {
var ret = 123;
return ret; // expect error
}

View File

@@ -0,0 +1,7 @@
interface WithNonPrimitive {
foo: object
}
var a: WithNonPrimitive = { foo: {bar: "bar"} };
var b: WithNonPrimitive = {foo: "bar"}; // expect error

View File

@@ -0,0 +1,17 @@
var x = {};
var y = {foo: "bar"};
var a: object;
x = a;
y = a; // expect error
var n = 123;
var b = true;
var s = "fooo";
a = n; // expect error
a = b; // expect error
a = s; // expect error
n = a; // expect error
b = a; // expect error
s = a; // expect error

View File

@@ -0,0 +1,15 @@
function generic<T>(t: T) {}
var a = {};
var b = "42";
generic<object>({});
generic<object>(a);
generic<object>(123); // expect error
generic<object>(b); // expect error
function bound<T extends object>(t: T) {}
bound({});
bound(a);
bound(123); // expect error
bound(b); // expect error

View File

@@ -0,0 +1,4 @@
var a: object & string = ""; // error
var b: object | string = ""; // ok
a = b; // error
b = a; // ok

View File

@@ -3,4 +3,5 @@ type any = I;
type number = I;
type boolean = I;
type string = I;
type void = I;
type void = I;
type object = I;