From ab60d319b2047c26cd1c73a4fd59012cc99a62fb Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 7 Feb 2017 14:07:55 -0800 Subject: [PATCH] Fix #13932: Change the order of overloads and allow union for `Object.create` --- src/lib/es5.d.ts | 16 +-- .../reference/objectCreate-errors.errors.txt | 38 +++++++ .../reference/objectCreate-errors.js | 22 ++++ tests/baselines/reference/objectCreate.js | 28 +++++ .../baselines/reference/objectCreate.symbols | 73 +++++++++++++ tests/baselines/reference/objectCreate.types | 103 ++++++++++++++++++ tests/baselines/reference/objectCreate2.js | 28 +++++ .../baselines/reference/objectCreate2.symbols | 73 +++++++++++++ tests/baselines/reference/objectCreate2.types | 103 ++++++++++++++++++ tests/cases/compiler/objectCreate-errors.ts | 12 ++ tests/cases/compiler/objectCreate.ts | 15 +++ tests/cases/compiler/objectCreate2.ts | 15 +++ 12 files changed, 518 insertions(+), 8 deletions(-) create mode 100644 tests/baselines/reference/objectCreate-errors.errors.txt create mode 100644 tests/baselines/reference/objectCreate-errors.js create mode 100644 tests/baselines/reference/objectCreate.js create mode 100644 tests/baselines/reference/objectCreate.symbols create mode 100644 tests/baselines/reference/objectCreate.types create mode 100644 tests/baselines/reference/objectCreate2.js create mode 100644 tests/baselines/reference/objectCreate2.symbols create mode 100644 tests/baselines/reference/objectCreate2.types create mode 100644 tests/cases/compiler/objectCreate-errors.ts create mode 100644 tests/cases/compiler/objectCreate.ts create mode 100644 tests/cases/compiler/objectCreate2.ts diff --git a/src/lib/es5.d.ts b/src/lib/es5.d.ts index 038d0344fda..516bc890d4c 100644 --- a/src/lib/es5.d.ts +++ b/src/lib/es5.d.ts @@ -137,17 +137,17 @@ 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. + * @param o Object to use as a prototype. */ create(o: T): T; + /** + * 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(o: T | null): T | object; + /** * 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 diff --git a/tests/baselines/reference/objectCreate-errors.errors.txt b/tests/baselines/reference/objectCreate-errors.errors.txt new file mode 100644 index 00000000000..38b9158351e --- /dev/null +++ b/tests/baselines/reference/objectCreate-errors.errors.txt @@ -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'. \ No newline at end of file diff --git a/tests/baselines/reference/objectCreate-errors.js b/tests/baselines/reference/objectCreate-errors.js new file mode 100644 index 00000000000..959b2f9473d --- /dev/null +++ b/tests/baselines/reference/objectCreate-errors.js @@ -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 diff --git a/tests/baselines/reference/objectCreate.js b/tests/baselines/reference/objectCreate.js new file mode 100644 index 00000000000..728fa8ca21a --- /dev/null +++ b/tests/baselines/reference/objectCreate.js @@ -0,0 +1,28 @@ +//// [objectCreate.ts] + +declare var union: null | { a: number, b: string }; + +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({}, {}); + + +//// [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({}, {}); diff --git a/tests/baselines/reference/objectCreate.symbols b/tests/baselines/reference/objectCreate.symbols new file mode 100644 index 00000000000..44cee102787 --- /dev/null +++ b/tests/baselines/reference/objectCreate.symbols @@ -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, --, --), 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, --, --), 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, --, --), 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, --, --), 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, --, --), 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, --, --), 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, --, --), 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, --, --), Decl(lib.d.ts, --, --)) + +var o = Object.create({}); // object +>o : Symbol(o, Decl(objectCreate.ts, 7, 3)) +>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), 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, --, --), 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, --, --), 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, --, --), 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, --, --), 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, --, --), 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, --, --), 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, --, --), 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, --, --), 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, --, --), Decl(lib.d.ts, --, --)) + +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, --, --), 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, --, --), Decl(lib.d.ts, --, --)) + diff --git a/tests/baselines/reference/objectCreate.types b/tests/baselines/reference/objectCreate.types new file mode 100644 index 00000000000..3c698cf54ff --- /dev/null +++ b/tests/baselines/reference/objectCreate.types @@ -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 : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (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 : { a: number; b: string; } +>Object.create({ a: 1, b: "" }) : { a: number; b: string; } +>Object.create : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (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 : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>union : { a: number; b: string; } | null + +var e = Object.create({}); // {} +>e : {} +>Object.create({}) : {} +>Object.create : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>{} : {} + +var o = Object.create({}); // object +>o : object +>Object.create({}) : object +>Object.create : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>{} : object +>{} : {} + +var a = Object.create(null, {}); // any +>a : any +>Object.create(null, {}) : any +>Object.create : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (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 : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (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 : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (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 : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>{} : {} +>{} : {} + +var a = Object.create({}, {}); +>a : any +>Object.create({}, {}) : any +>Object.create : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T | null): object | T; (o: object | null, properties: PropertyDescriptorMap): any; } +>{} : object +>{} : {} +>{} : {} + diff --git a/tests/baselines/reference/objectCreate2.js b/tests/baselines/reference/objectCreate2.js new file mode 100644 index 00000000000..cfc79c2ef35 --- /dev/null +++ b/tests/baselines/reference/objectCreate2.js @@ -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 + +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({}, {}); + + +//// [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({}, {}); diff --git a/tests/baselines/reference/objectCreate2.symbols b/tests/baselines/reference/objectCreate2.symbols new file mode 100644 index 00000000000..e10db7899c1 --- /dev/null +++ b/tests/baselines/reference/objectCreate2.symbols @@ -0,0 +1,73 @@ +=== tests/cases/compiler/objectCreate2.ts === + +declare var union: null | { a: number, b: string }; +>union : Symbol(union, Decl(objectCreate2.ts, 1, 11)) +>a : Symbol(a, Decl(objectCreate2.ts, 1, 27)) +>b : Symbol(b, Decl(objectCreate2.ts, 1, 38)) + +var n = Object.create(null); // any +>n : Symbol(n, Decl(objectCreate2.ts, 3, 3)) +>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), 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, --, --), Decl(lib.d.ts, --, --)) + +var t = Object.create({ a: 1, b: "" }); // {a: number, b: string } +>t : Symbol(t, Decl(objectCreate2.ts, 4, 3)) +>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), 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, --, --), Decl(lib.d.ts, --, --)) +>a : Symbol(a, Decl(objectCreate2.ts, 4, 23)) +>b : Symbol(b, Decl(objectCreate2.ts, 4, 29)) + +var u = Object.create(union); // {a: number, b: string } +>u : Symbol(u, Decl(objectCreate2.ts, 5, 3)) +>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), 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, --, --), Decl(lib.d.ts, --, --)) +>union : Symbol(union, Decl(objectCreate2.ts, 1, 11)) + +var e = Object.create({}); // {} +>e : Symbol(e, Decl(objectCreate2.ts, 6, 3)) +>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), 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, --, --), Decl(lib.d.ts, --, --)) + +var o = Object.create({}); // object +>o : Symbol(o, Decl(objectCreate2.ts, 7, 3)) +>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), 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, --, --), Decl(lib.d.ts, --, --)) + +var a = Object.create(null, {}); // any +>a : Symbol(a, Decl(objectCreate2.ts, 9, 3), Decl(objectCreate2.ts, 10, 3), Decl(objectCreate2.ts, 11, 3), Decl(objectCreate2.ts, 12, 3), Decl(objectCreate2.ts, 13, 3)) +>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), 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, --, --), Decl(lib.d.ts, --, --)) + +var a = Object.create({ a: 1, b: "" }, {}); +>a : Symbol(a, Decl(objectCreate2.ts, 9, 3), Decl(objectCreate2.ts, 10, 3), Decl(objectCreate2.ts, 11, 3), Decl(objectCreate2.ts, 12, 3), Decl(objectCreate2.ts, 13, 3)) +>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), 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, --, --), Decl(lib.d.ts, --, --)) +>a : Symbol(a, Decl(objectCreate2.ts, 10, 23)) +>b : Symbol(b, Decl(objectCreate2.ts, 10, 29)) + +var a = Object.create(union, {}); +>a : Symbol(a, Decl(objectCreate2.ts, 9, 3), Decl(objectCreate2.ts, 10, 3), Decl(objectCreate2.ts, 11, 3), Decl(objectCreate2.ts, 12, 3), Decl(objectCreate2.ts, 13, 3)) +>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), 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, --, --), Decl(lib.d.ts, --, --)) +>union : Symbol(union, Decl(objectCreate2.ts, 1, 11)) + +var a = Object.create({}, {}); +>a : Symbol(a, Decl(objectCreate2.ts, 9, 3), Decl(objectCreate2.ts, 10, 3), Decl(objectCreate2.ts, 11, 3), Decl(objectCreate2.ts, 12, 3), Decl(objectCreate2.ts, 13, 3)) +>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), 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, --, --), Decl(lib.d.ts, --, --)) + +var a = Object.create({}, {}); +>a : Symbol(a, Decl(objectCreate2.ts, 9, 3), Decl(objectCreate2.ts, 10, 3), Decl(objectCreate2.ts, 11, 3), Decl(objectCreate2.ts, 12, 3), Decl(objectCreate2.ts, 13, 3)) +>Object.create : Symbol(ObjectConstructor.create, Decl(lib.d.ts, --, --), 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, --, --), Decl(lib.d.ts, --, --)) + diff --git a/tests/baselines/reference/objectCreate2.types b/tests/baselines/reference/objectCreate2.types new file mode 100644 index 00000000000..1066688772b --- /dev/null +++ b/tests/baselines/reference/objectCreate2.types @@ -0,0 +1,103 @@ +=== tests/cases/compiler/objectCreate2.ts === + +declare var union: null | { a: number, b: string }; +>union : { a: number; b: string; } +>null : null +>a : number +>b : string + +var n = Object.create(null); // any +>n : any +>Object.create(null) : any +>Object.create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>null : null + +var t = Object.create({ a: 1, b: "" }); // {a: number, b: string } +>t : { a: number; b: string; } +>Object.create({ a: 1, b: "" }) : { a: number; b: string; } +>Object.create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>{ a: 1, b: "" } : { a: number; b: string; } +>a : number +>1 : 1 +>b : string +>"" : "" + +var u = Object.create(union); // {a: number, b: string } +>u : { a: number; b: string; } +>Object.create(union) : { a: number; b: string; } +>Object.create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>union : { a: number; b: string; } + +var e = Object.create({}); // {} +>e : {} +>Object.create({}) : {} +>Object.create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>{} : {} + +var o = Object.create({}); // object +>o : object +>Object.create({}) : object +>Object.create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>{} : object +>{} : {} + +var a = Object.create(null, {}); // any +>a : any +>Object.create(null, {}) : any +>Object.create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>null : null +>{} : {} + +var a = Object.create({ a: 1, b: "" }, {}); +>a : any +>Object.create({ a: 1, b: "" }, {}) : any +>Object.create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T): object | T; (o: object, 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 : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>union : { a: number; b: string; } +>{} : {} + +var a = Object.create({}, {}); +>a : any +>Object.create({}, {}) : any +>Object.create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>{} : {} +>{} : {} + +var a = Object.create({}, {}); +>a : any +>Object.create({}, {}) : any +>Object.create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>Object : ObjectConstructor +>create : { (o: T): T; (o: T): object | T; (o: object, properties: PropertyDescriptorMap): any; } +>{} : object +>{} : {} +>{} : {} + diff --git a/tests/cases/compiler/objectCreate-errors.ts b/tests/cases/compiler/objectCreate-errors.ts new file mode 100644 index 00000000000..ea7b8bd95ab --- /dev/null +++ b/tests/cases/compiler/objectCreate-errors.ts @@ -0,0 +1,12 @@ +// @strictNullChecks: true + +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 \ No newline at end of file diff --git a/tests/cases/compiler/objectCreate.ts b/tests/cases/compiler/objectCreate.ts new file mode 100644 index 00000000000..a497ff6b95b --- /dev/null +++ b/tests/cases/compiler/objectCreate.ts @@ -0,0 +1,15 @@ +// @strictNullChecks: true + +declare var union: null | { a: number, b: string }; + +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({}, {}); diff --git a/tests/cases/compiler/objectCreate2.ts b/tests/cases/compiler/objectCreate2.ts new file mode 100644 index 00000000000..d28adaae32e --- /dev/null +++ b/tests/cases/compiler/objectCreate2.ts @@ -0,0 +1,15 @@ +// @strictNullChecks: false + +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 + +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({}, {});