diff --git a/docs/diff/es2017.object.d.ts.md b/docs/diff/es2017.object.d.ts.md index 12deef8..2604307 100644 --- a/docs/diff/es2017.object.d.ts.md +++ b/docs/diff/es2017.object.d.ts.md @@ -5,7 +5,7 @@ Index: es2017.object.d.ts =================================================================== --- es2017.object.d.ts +++ es2017.object.d.ts -@@ -2,34 +2,44 @@ +@@ -2,34 +2,48 @@ /** * Returns an array of values of the enumerable properties of an object * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. @@ -18,7 +18,9 @@ Index: es2017.object.d.ts * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. */ - values(o: {}): any[]; -+ values(o: Record): V[]; ++ values( ++ o: Record, ++ ): (string extends K ? V : number extends K ? V : unknown)[]; + /** + * Returns an array of values of the enumerable properties of an object + * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. @@ -37,7 +39,9 @@ Index: es2017.object.d.ts * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. */ - entries(o: {}): [string, any][]; -+ entries(o: Record): [string, V][]; ++ entries( ++ o: Record, ++ ): [string, string extends K ? V : number extends K ? V : unknown][]; + /** + * Returns an array of key/values of the enumerable properties of an object + * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. diff --git a/generated/lib.es2017.object.d.ts b/generated/lib.es2017.object.d.ts index 09da6b7..1ed0bd2 100644 --- a/generated/lib.es2017.object.d.ts +++ b/generated/lib.es2017.object.d.ts @@ -9,7 +9,9 @@ interface ObjectConstructor { * Returns an array of values of the enumerable properties of an object * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. */ - values(o: Record): V[]; + values( + o: Record, + ): (string extends K ? V : number extends K ? V : unknown)[]; /** * Returns an array of values of the enumerable properties of an object * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. @@ -25,7 +27,9 @@ interface ObjectConstructor { * Returns an array of key/values of the enumerable properties of an object * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. */ - entries(o: Record): [string, V][]; + entries( + o: Record, + ): [string, string extends K ? V : number extends K ? V : unknown][]; /** * Returns an array of key/values of the enumerable properties of an object * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. diff --git a/lib/lib.es2017.object.d.ts b/lib/lib.es2017.object.d.ts index 9cb187d..6b73519 100644 --- a/lib/lib.es2017.object.d.ts +++ b/lib/lib.es2017.object.d.ts @@ -8,7 +8,9 @@ interface ObjectConstructor { * Returns an array of values of the enumerable properties of an object * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. */ - values(o: Record): V[]; + values( + o: Record, + ): (string extends K ? V : number extends K ? V : unknown)[]; /** * Returns an array of values of the enumerable properties of an object * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. @@ -24,7 +26,9 @@ interface ObjectConstructor { * Returns an array of key/values of the enumerable properties of an object * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. */ - entries(o: Record): [string, V][]; + entries( + o: Record, + ): [string, string extends K ? V : number extends K ? V : unknown][]; /** * Returns an array of key/values of the enumerable properties of an object * @param o Object that contains the properties and methods. This can be an object that you created or an existing Document Object Model (DOM) object. diff --git a/tests/src/es2017.object.ts b/tests/src/es2017.object.ts index b7753b9..841a699 100644 --- a/tests/src/es2017.object.ts +++ b/tests/src/es2017.object.ts @@ -33,14 +33,14 @@ function createGenericRecord( const obj4 = createGenericRecord(["foo", "bar", "baz"], [1, 2, 3]); const values4 = Object.values(obj4); const entries4 = Object.entries(obj4); - expectType(values4); - expectType<[string, number][]>(entries4); + expectType(values4); + expectType<[string, unknown][]>(entries4); const obj5 = createGenericRecord(["foo", "bar", "baz"], [1, obj1, 3]); const values5 = Object.values(obj5); const entries5 = Object.entries(obj5); - expectType<(number | { [k: string]: number })[]>(values5); - expectType<[string, number | { [k: string]: number }][]>(entries5); + expectType(values5); + expectType<[string, unknown][]>(entries5); } function test(obj: Record) { const values = Object.values(obj); @@ -50,3 +50,19 @@ function test(obj: Record) { expectType(entries[0][0]); expectType(entries[0][1]); } + +{ + // https://github.com/uhyo/better-typescript-lib/issues/40 + const obj: {} = {}; + const obj2 = { foo: 123 }; + const values = Object.values(obj); + const values2 = Object.values(obj2); + expectType(values); + expectType(values2); + + const entries = Object.entries(obj); + const entries2 = Object.entries(obj2); + + expectType<[string, unknown][]>(entries); + expectType<[string, unknown][]>(entries2); +}