Skip to content

Commit 4348e7b

Browse files
authored
fix: tighten up Object.values and Object.entries types to infer unknwon in more cases (#42)
1 parent e2bee5f commit 4348e7b

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

docs/diff/es2017.object.d.ts.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Index: es2017.object.d.ts
55
===================================================================
66
--- es2017.object.d.ts
77
+++ es2017.object.d.ts
8-
@@ -2,34 +2,44 @@
8+
@@ -2,34 +2,48 @@
99
/**
1010
* Returns an array of values of the enumerable properties of an object
1111
* @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
1818
* @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.
1919
*/
2020
- values(o: {}): any[];
21-
+ values<K extends PropertyKey, V>(o: Record<K, V>): V[];
21+
+ values<K extends PropertyKey, V>(
22+
+ o: Record<K, V>,
23+
+ ): (string extends K ? V : number extends K ? V : unknown)[];
2224
+ /**
2325
+ * Returns an array of values of the enumerable properties of an object
2426
+ * @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
3739
* @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.
3840
*/
3941
- entries(o: {}): [string, any][];
40-
+ entries<K extends PropertyKey, V>(o: Record<K, V>): [string, V][];
42+
+ entries<K extends PropertyKey, V>(
43+
+ o: Record<K, V>,
44+
+ ): [string, string extends K ? V : number extends K ? V : unknown][];
4145
+ /**
4246
+ * Returns an array of key/values of the enumerable properties of an object
4347
+ * @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.

generated/lib.es2017.object.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ interface ObjectConstructor {
99
* Returns an array of values of the enumerable properties of an object
1010
* @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.
1111
*/
12-
values<K extends PropertyKey, V>(o: Record<K, V>): V[];
12+
values<K extends PropertyKey, V>(
13+
o: Record<K, V>,
14+
): (string extends K ? V : number extends K ? V : unknown)[];
1315
/**
1416
* Returns an array of values of the enumerable properties of an object
1517
* @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 {
2527
* Returns an array of key/values of the enumerable properties of an object
2628
* @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.
2729
*/
28-
entries<K extends PropertyKey, V>(o: Record<K, V>): [string, V][];
30+
entries<K extends PropertyKey, V>(
31+
o: Record<K, V>,
32+
): [string, string extends K ? V : number extends K ? V : unknown][];
2933
/**
3034
* Returns an array of key/values of the enumerable properties of an object
3135
* @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.

lib/lib.es2017.object.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ interface ObjectConstructor {
88
* Returns an array of values of the enumerable properties of an object
99
* @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.
1010
*/
11-
values<K extends PropertyKey, V>(o: Record<K, V>): V[];
11+
values<K extends PropertyKey, V>(
12+
o: Record<K, V>,
13+
): (string extends K ? V : number extends K ? V : unknown)[];
1214
/**
1315
* Returns an array of values of the enumerable properties of an object
1416
* @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 {
2426
* Returns an array of key/values of the enumerable properties of an object
2527
* @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.
2628
*/
27-
entries<K extends PropertyKey, V>(o: Record<K, V>): [string, V][];
29+
entries<K extends PropertyKey, V>(
30+
o: Record<K, V>,
31+
): [string, string extends K ? V : number extends K ? V : unknown][];
2832
/**
2933
* Returns an array of key/values of the enumerable properties of an object
3034
* @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.

tests/src/es2017.object.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ function createGenericRecord<K extends string, V>(
3333
const obj4 = createGenericRecord(["foo", "bar", "baz"], [1, 2, 3]);
3434
const values4 = Object.values(obj4);
3535
const entries4 = Object.entries(obj4);
36-
expectType<number[]>(values4);
37-
expectType<[string, number][]>(entries4);
36+
expectType<unknown[]>(values4);
37+
expectType<[string, unknown][]>(entries4);
3838

3939
const obj5 = createGenericRecord(["foo", "bar", "baz"], [1, obj1, 3]);
4040
const values5 = Object.values(obj5);
4141
const entries5 = Object.entries(obj5);
42-
expectType<(number | { [k: string]: number })[]>(values5);
43-
expectType<[string, number | { [k: string]: number }][]>(entries5);
42+
expectType<unknown[]>(values5);
43+
expectType<[string, unknown][]>(entries5);
4444
}
4545
function test(obj: Record<string, unknown>) {
4646
const values = Object.values(obj);
@@ -50,3 +50,19 @@ function test(obj: Record<string, unknown>) {
5050
expectType<string>(entries[0][0]);
5151
expectType<unknown>(entries[0][1]);
5252
}
53+
54+
{
55+
// https://github.com/uhyo/better-typescript-lib/issues/40
56+
const obj: {} = {};
57+
const obj2 = { foo: 123 };
58+
const values = Object.values(obj);
59+
const values2 = Object.values(obj2);
60+
expectType<unknown[]>(values);
61+
expectType<unknown[]>(values2);
62+
63+
const entries = Object.entries(obj);
64+
const entries2 = Object.entries(obj2);
65+
66+
expectType<[string, unknown][]>(entries);
67+
expectType<[string, unknown][]>(entries2);
68+
}

0 commit comments

Comments
 (0)