Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tighten up Object.values and Object.entries types #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions docs/diff/es2017.object.d.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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<K extends PropertyKey, V>(o: Record<K, V>): V[];
+ values<K extends PropertyKey, V>(
+ o: Record<K, V>,
+ ): (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.
Expand All @@ -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<K extends PropertyKey, V>(o: Record<K, V>): [string, V][];
+ entries<K extends PropertyKey, V>(
+ o: Record<K, V>,
+ ): [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.
Expand Down
8 changes: 6 additions & 2 deletions generated/lib.es2017.object.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<K extends PropertyKey, V>(o: Record<K, V>): V[];
values<K extends PropertyKey, V>(
o: Record<K, V>,
): (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.
Expand All @@ -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<K extends PropertyKey, V>(o: Record<K, V>): [string, V][];
entries<K extends PropertyKey, V>(
o: Record<K, V>,
): [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.
Expand Down
8 changes: 6 additions & 2 deletions lib/lib.es2017.object.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<K extends PropertyKey, V>(o: Record<K, V>): V[];
values<K extends PropertyKey, V>(
o: Record<K, V>,
): (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.
Expand All @@ -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<K extends PropertyKey, V>(o: Record<K, V>): [string, V][];
entries<K extends PropertyKey, V>(
o: Record<K, V>,
): [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.
Expand Down
24 changes: 20 additions & 4 deletions tests/src/es2017.object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ function createGenericRecord<K extends string, V>(
const obj4 = createGenericRecord(["foo", "bar", "baz"], [1, 2, 3]);
const values4 = Object.values(obj4);
const entries4 = Object.entries(obj4);
expectType<number[]>(values4);
expectType<[string, number][]>(entries4);
expectType<unknown[]>(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<unknown[]>(values5);
expectType<[string, unknown][]>(entries5);
}
function test(obj: Record<string, unknown>) {
const values = Object.values(obj);
Expand All @@ -50,3 +50,19 @@ function test(obj: Record<string, unknown>) {
expectType<string>(entries[0][0]);
expectType<unknown>(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<unknown[]>(values);
expectType<unknown[]>(values2);

const entries = Object.entries(obj);
const entries2 = Object.entries(obj2);

expectType<[string, unknown][]>(entries);
expectType<[string, unknown][]>(entries2);
}
Loading