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

[objasinterface] Make sure that keys/values/entries are typed to work against class instances too #329

Merged
merged 2 commits into from
Jul 14, 2022
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
5 changes: 5 additions & 0 deletions .changeset/nice-shirts-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/wonder-stuff-core": patch
---

Broaden typing for keys/values/entries methods
12 changes: 12 additions & 0 deletions packages/wonder-stuff-core/src/__tests__/entries.flowtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,15 @@ import {entries} from "../entries.js";
// $FlowExpectedError[incompatible-type]
const [___, ____]: [string, number] = entries1[0];
}

{
// should work with class instances
class Foo {
a: string;
b: string;
}
const foo = new Foo();

// This should not be erroring.
const _ = entries(foo);
}
12 changes: 12 additions & 0 deletions packages/wonder-stuff-core/src/__tests__/keys.flowtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,15 @@ import {keys} from "../keys.js";
// This should not be erroring.
const _ = keys(obj3);
}

{
// should work with class instances
class Foo {
a: string;
b: string;
}
const foo = new Foo();

// This should not be erroring.
const _ = keys(foo);
}
12 changes: 12 additions & 0 deletions packages/wonder-stuff-core/src/__tests__/values.flowtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@ import {values} from "../values.js";
// $FlowExpectedError[incompatible-call]
const __ = values<number>(obj2);
}

{
// should work with class instances
class Foo {
a: string;
b: string;
}
const foo = new Foo();

// This should not be erroring.
const _ = values(foo);
}
6 changes: 4 additions & 2 deletions packages/wonder-stuff-core/src/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
/**
* Return an array of key/value tuples for an object.
*
* @param {$ReadOnly<{[K]: V}>} obj The object for which the values are
* @param {$ReadOnly<interface {[K]: V}>} obj The object for which the values are
* to be returned.
* @returns {Array<[K, V]>} An array of key/value tuples for the object.
*/
export function entries<K, V>(obj: $ReadOnly<{[K]: V}>): Array<[K, V]> {
export function entries<K, V>(
obj: $ReadOnly<interface {[K]: V}>,
): Array<[K, V]> {
// This cast is deliberate as Object.entries is typed to return
// Array<[string, mixed]>, but we want to return Array<[K, V]>.
// $FlowIgnore[unclear-type]
Expand Down
6 changes: 4 additions & 2 deletions packages/wonder-stuff-core/src/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
/**
* Return an array of the enumerable keys of an object.
*
* @param {$ReadOnly<{[string]: mixed}>} obj The object for which the values are
* @param {$ReadOnly<interface {[string]: mixed}>} obj The object for which the values are
* to be returned.
* @returns {Array<$Keys<O>>} An array of the enumerable keys of an object.
*/
export function keys<O: {[string]: mixed}>(obj: $ReadOnly<O>): Array<$Keys<O>> {
export function keys<O: interface {[string]: mixed}>(
obj: $ReadOnly<O>,
): Array<$Keys<O>> {
return Object.keys(obj);
}
4 changes: 2 additions & 2 deletions packages/wonder-stuff-core/src/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
/**
* Return an array of the enumerable property values of an object.
*
* @param {$ReadOnly<{[mixed]: V}>} obj The object for which the values are
* @param {$ReadOnly<interface {[mixed]: V}>} obj The object for which the values are
* to be returned.
* @returns {Array<V>} An array of the enumerable property values of the object.
*/
export function values<V>(obj: $ReadOnly<{|[mixed]: V|}>): Array<V> {
export function values<V>(obj: $ReadOnly<interface {[mixed]: V}>): Array<V> {
// This is a deliberate cast through any.
// Object.values returns Array<mixed> and we want to return Array<V>.
// $FlowIgnore[unclear-type]
Expand Down