Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,16 +137,10 @@ interface ObjectConstructor {
getOwnPropertyNames(o: any): string[];

/**
* Creates an object that has null prototype.
* @param o Object to use as a prototype. May be null
*/
create(o: null): any;

/**
* Creates an object that has the specified prototype, and that optionally contains specified properties.
* @param o Object to use as a prototype. May be null
* Creates an object that has the specified prototype or that has null prototype.
* @param o Object to use as a prototype. May be null.
*/
create<T extends object>(o: T): T;
create<T extends object>(o: T | null): T | object;

/**
* Creates an object that has the specified prototype, and that optionally contains specified properties.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(3,1): error TS2322: Type 'Object' is not assignable to type 'RegExp'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Property 'exec' is missing in type 'Object'.
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(5,5): error TS2322: Type 'Object' is not assignable to type 'String'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Property 'charAt' is missing in type 'Object'.
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(6,5): error TS2322: Type 'Number' is not assignable to type 'String'.
Property 'charAt' is missing in type 'Number'.
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(5,5): error TS2322: Type 'object | Object' is not assignable to type 'String'.
Type 'object' is not assignable to type 'String'.
Property 'charAt' is missing in type '{}'.
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(6,5): error TS2322: Type 'object | Number' is not assignable to type 'String'.
Type 'object' is not assignable to type 'String'.
tests/cases/compiler/assigningFromObjectToAnythingElse.ts(8,5): error TS2322: Type 'Object' is not assignable to type 'Error'.
The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
Property 'name' is missing in type 'Object'.
Expand All @@ -22,13 +22,13 @@ tests/cases/compiler/assigningFromObjectToAnythingElse.ts(8,5): error TS2322: Ty

var a: String = Object.create<Object>("");
~
!!! error TS2322: Type 'Object' is not assignable to type 'String'.
!!! error TS2322: The 'Object' type is assignable to very few other types. Did you mean to use the 'any' type instead?
!!! error TS2322: Property 'charAt' is missing in type 'Object'.
!!! error TS2322: Type 'object | Object' is not assignable to type 'String'.
!!! error TS2322: Type 'object' is not assignable to type 'String'.
!!! error TS2322: Property 'charAt' is missing in type '{}'.
var c: String = Object.create<Number>(1);
~
!!! error TS2322: Type 'Number' is not assignable to type 'String'.
!!! error TS2322: Property 'charAt' is missing in type 'Number'.
!!! error TS2322: Type 'object | Number' is not assignable to type 'String'.
!!! error TS2322: Type 'object' is not assignable to type 'String'.

var w: Error = new Object();
~
Expand Down
38 changes: 38 additions & 0 deletions tests/baselines/reference/objectCreate-errors.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
tests/cases/compiler/objectCreate-errors.ts(2,24): error TS2345: Argument of type '1' is not assignable to parameter of type 'object | null'.
tests/cases/compiler/objectCreate-errors.ts(3,24): error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object | null'.
tests/cases/compiler/objectCreate-errors.ts(4,24): error TS2345: Argument of type 'false' is not assignable to parameter of type 'object | null'.
tests/cases/compiler/objectCreate-errors.ts(5,24): error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'object | null'.
tests/cases/compiler/objectCreate-errors.ts(8,24): error TS2345: Argument of type '1' is not assignable to parameter of type 'object | null'.
tests/cases/compiler/objectCreate-errors.ts(9,24): error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object | null'.
tests/cases/compiler/objectCreate-errors.ts(10,24): error TS2345: Argument of type 'false' is not assignable to parameter of type 'object | null'.
tests/cases/compiler/objectCreate-errors.ts(11,24): error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'object | null'.


==== tests/cases/compiler/objectCreate-errors.ts (8 errors) ====

var e1 = Object.create(1); // Error
~
!!! error TS2345: Argument of type '1' is not assignable to parameter of type 'object | null'.
var e2 = Object.create("string"); // Error
~~~~~~~~
!!! error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object | null'.
var e3 = Object.create(false); // Error
~~~~~
!!! error TS2345: Argument of type 'false' is not assignable to parameter of type 'object | null'.
var e4 = Object.create(undefined); // Error
~~~~~~~~~
!!! error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'object | null'.


var e5 = Object.create(1, {}); // Error
~
!!! error TS2345: Argument of type '1' is not assignable to parameter of type 'object | null'.
var e6 = Object.create("string", {}); // Error
~~~~~~~~
!!! error TS2345: Argument of type '"string"' is not assignable to parameter of type 'object | null'.
var e7 = Object.create(false, {}); // Error
~~~~~
!!! error TS2345: Argument of type 'false' is not assignable to parameter of type 'object | null'.
var e8 = Object.create(undefined, {}); // Error
~~~~~~~~~
!!! error TS2345: Argument of type 'undefined' is not assignable to parameter of type 'object | null'.
22 changes: 22 additions & 0 deletions tests/baselines/reference/objectCreate-errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//// [objectCreate-errors.ts]

var e1 = Object.create(1); // Error
var e2 = Object.create("string"); // Error
var e3 = Object.create(false); // Error
var e4 = Object.create(undefined); // Error


var e5 = Object.create(1, {}); // Error
var e6 = Object.create("string", {}); // Error
var e7 = Object.create(false, {}); // Error
var e8 = Object.create(undefined, {}); // Error

//// [objectCreate-errors.js]
var e1 = Object.create(1); // Error
var e2 = Object.create("string"); // Error
var e3 = Object.create(false); // Error
var e4 = Object.create(undefined); // Error
var e5 = Object.create(1, {}); // Error
var e6 = Object.create("string", {}); // Error
var e7 = Object.create(false, {}); // Error
var e8 = Object.create(undefined, {}); // Error
28 changes: 28 additions & 0 deletions tests/baselines/reference/objectCreate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [objectCreate.ts]

declare var union: null | { a: number, b: string };

var n = Object.create(null); // object

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a 'well defined' object, namely {}?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in an input {} is not the same as object. object is more specific than {}. in here it does not matter. i used object to avoid having a type of {} | object.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Thank you. (:

var t = Object.create({ a: 1, b: "" }); // {a: number, b: string }
var u = Object.create(union); // object | {a: number, b: string }
var e = Object.create({}); // {}
var o = Object.create(<object>{}); // object

var a = Object.create(null, {}); // any
var a = Object.create({ a: 1, b: "" }, {});
var a = Object.create(union, {});
var a = Object.create({}, {});
var a = Object.create(<object>{}, {});


//// [objectCreate.js]
var n = Object.create(null); // object
var t = Object.create({ a: 1, b: "" }); // {a: number, b: string }
var u = Object.create(union); // object | {a: number, b: string }
var e = Object.create({}); // {}
var o = Object.create({}); // object
var a = Object.create(null, {}); // any
var a = Object.create({ a: 1, b: "" }, {});
var a = Object.create(union, {});
var a = Object.create({}, {});
var a = Object.create({}, {});
73 changes: 73 additions & 0 deletions tests/baselines/reference/objectCreate.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
=== tests/cases/compiler/objectCreate.ts ===

declare var union: null | { a: number, b: string };
>union : Symbol(union, Decl(objectCreate.ts, 1, 11))
>a : Symbol(a, Decl(objectCreate.ts, 1, 27))
>b : Symbol(b, Decl(objectCreate.ts, 1, 38))

var n = Object.create(null); // object
>n : Symbol(n, Decl(objectCreate.ts, 3, 3))
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

var t = Object.create({ a: 1, b: "" }); // {a: number, b: string }
>t : Symbol(t, Decl(objectCreate.ts, 4, 3))
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>a : Symbol(a, Decl(objectCreate.ts, 4, 23))
>b : Symbol(b, Decl(objectCreate.ts, 4, 29))

var u = Object.create(union); // object | {a: number, b: string }
>u : Symbol(u, Decl(objectCreate.ts, 5, 3))
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>union : Symbol(union, Decl(objectCreate.ts, 1, 11))

var e = Object.create({}); // {}
>e : Symbol(e, Decl(objectCreate.ts, 6, 3))
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

var o = Object.create(<object>{}); // object
>o : Symbol(o, Decl(objectCreate.ts, 7, 3))
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

var a = Object.create(null, {}); // any
>a : Symbol(a, Decl(objectCreate.ts, 9, 3), Decl(objectCreate.ts, 10, 3), Decl(objectCreate.ts, 11, 3), Decl(objectCreate.ts, 12, 3), Decl(objectCreate.ts, 13, 3))
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

var a = Object.create({ a: 1, b: "" }, {});
>a : Symbol(a, Decl(objectCreate.ts, 9, 3), Decl(objectCreate.ts, 10, 3), Decl(objectCreate.ts, 11, 3), Decl(objectCreate.ts, 12, 3), Decl(objectCreate.ts, 13, 3))
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>a : Symbol(a, Decl(objectCreate.ts, 10, 23))
>b : Symbol(b, Decl(objectCreate.ts, 10, 29))

var a = Object.create(union, {});
>a : Symbol(a, Decl(objectCreate.ts, 9, 3), Decl(objectCreate.ts, 10, 3), Decl(objectCreate.ts, 11, 3), Decl(objectCreate.ts, 12, 3), Decl(objectCreate.ts, 13, 3))
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>union : Symbol(union, Decl(objectCreate.ts, 1, 11))

var a = Object.create({}, {});
>a : Symbol(a, Decl(objectCreate.ts, 9, 3), Decl(objectCreate.ts, 10, 3), Decl(objectCreate.ts, 11, 3), Decl(objectCreate.ts, 12, 3), Decl(objectCreate.ts, 13, 3))
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

var a = Object.create(<object>{}, {});
>a : Symbol(a, Decl(objectCreate.ts, 9, 3), Decl(objectCreate.ts, 10, 3), Decl(objectCreate.ts, 11, 3), Decl(objectCreate.ts, 12, 3), Decl(objectCreate.ts, 13, 3))
>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>Object : Symbol(Object, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))
>create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --))

103 changes: 103 additions & 0 deletions tests/baselines/reference/objectCreate.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
=== tests/cases/compiler/objectCreate.ts ===

declare var union: null | { a: number, b: string };
>union : { a: number; b: string; } | null
>null : null
>a : number
>b : string

var n = Object.create(null); // object
>n : object
>Object.create(null) : object
>Object.create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>Object : ObjectConstructor
>create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>null : null

var t = Object.create({ a: 1, b: "" }); // {a: number, b: string }
>t : object | { a: number; b: string; }
>Object.create({ a: 1, b: "" }) : object | { a: number; b: string; }
>Object.create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>Object : ObjectConstructor
>create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>{ a: 1, b: "" } : { a: number; b: string; }
>a : number
>1 : 1
>b : string
>"" : ""

var u = Object.create(union); // object | {a: number, b: string }
>u : object | { a: number; b: string; }
>Object.create(union) : object | { a: number; b: string; }
>Object.create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>Object : ObjectConstructor
>create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>union : { a: number; b: string; } | null

var e = Object.create({}); // {}
>e : object | {}
>Object.create({}) : object | {}
>Object.create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>Object : ObjectConstructor
>create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>{} : {}

var o = Object.create(<object>{}); // object
>o : object
>Object.create(<object>{}) : object
>Object.create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>Object : ObjectConstructor
>create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
><object>{} : object
>{} : {}

var a = Object.create(null, {}); // any
>a : any
>Object.create(null, {}) : any
>Object.create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>Object : ObjectConstructor
>create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>null : null
>{} : {}

var a = Object.create({ a: 1, b: "" }, {});
>a : any
>Object.create({ a: 1, b: "" }, {}) : any
>Object.create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>Object : ObjectConstructor
>create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>{ a: 1, b: "" } : { a: number; b: string; }
>a : number
>1 : 1
>b : string
>"" : ""
>{} : {}

var a = Object.create(union, {});
>a : any
>Object.create(union, {}) : any
>Object.create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>Object : ObjectConstructor
>create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>union : { a: number; b: string; } | null
>{} : {}

var a = Object.create({}, {});
>a : any
>Object.create({}, {}) : any
>Object.create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>Object : ObjectConstructor
>create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>{} : {}
>{} : {}

var a = Object.create(<object>{}, {});
>a : any
>Object.create(<object>{}, {}) : any
>Object.create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
>Object : ObjectConstructor
>create : { <T extends object>(o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; }
><object>{} : object
>{} : {}
>{} : {}

28 changes: 28 additions & 0 deletions tests/baselines/reference/objectCreate2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [objectCreate2.ts]

declare var union: null | { a: number, b: string };

var n = Object.create(null); // any
var t = Object.create({ a: 1, b: "" }); // {a: number, b: string }
var u = Object.create(union); // {a: number, b: string }
var e = Object.create({}); // {}
var o = Object.create(<object>{}); // object

var a = Object.create(null, {}); // any
var a = Object.create({ a: 1, b: "" }, {});
var a = Object.create(union, {});
var a = Object.create({}, {});
var a = Object.create(<object>{}, {});


//// [objectCreate2.js]
var n = Object.create(null); // any
var t = Object.create({ a: 1, b: "" }); // {a: number, b: string }
var u = Object.create(union); // {a: number, b: string }
var e = Object.create({}); // {}
var o = Object.create({}); // object
var a = Object.create(null, {}); // any
var a = Object.create({ a: 1, b: "" }, {});
var a = Object.create(union, {});
var a = Object.create({}, {});
var a = Object.create({}, {});
Loading