diff --git a/.changeset/nice-shirts-switch.md b/.changeset/nice-shirts-switch.md new file mode 100644 index 00000000..2f0239c2 --- /dev/null +++ b/.changeset/nice-shirts-switch.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/wonder-stuff-core": patch +--- + +Broaden typing for keys/values/entries methods diff --git a/packages/wonder-stuff-core/src/__tests__/entries.flowtest.js b/packages/wonder-stuff-core/src/__tests__/entries.flowtest.js index da5cfa0c..10faa8e5 100644 --- a/packages/wonder-stuff-core/src/__tests__/entries.flowtest.js +++ b/packages/wonder-stuff-core/src/__tests__/entries.flowtest.js @@ -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); +} diff --git a/packages/wonder-stuff-core/src/__tests__/keys.flowtest.js b/packages/wonder-stuff-core/src/__tests__/keys.flowtest.js index accb40f6..14f95d13 100644 --- a/packages/wonder-stuff-core/src/__tests__/keys.flowtest.js +++ b/packages/wonder-stuff-core/src/__tests__/keys.flowtest.js @@ -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); +} diff --git a/packages/wonder-stuff-core/src/__tests__/values.flowtest.js b/packages/wonder-stuff-core/src/__tests__/values.flowtest.js index f232e55a..86198551 100644 --- a/packages/wonder-stuff-core/src/__tests__/values.flowtest.js +++ b/packages/wonder-stuff-core/src/__tests__/values.flowtest.js @@ -58,3 +58,15 @@ import {values} from "../values.js"; // $FlowExpectedError[incompatible-call] const __ = values(obj2); } + +{ + // should work with class instances + class Foo { + a: string; + b: string; + } + const foo = new Foo(); + + // This should not be erroring. + const _ = values(foo); +} diff --git a/packages/wonder-stuff-core/src/entries.js b/packages/wonder-stuff-core/src/entries.js index 584b7cbd..f6f05a7c 100644 --- a/packages/wonder-stuff-core/src/entries.js +++ b/packages/wonder-stuff-core/src/entries.js @@ -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} 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(obj: $ReadOnly<{[K]: V}>): Array<[K, V]> { +export function entries( + obj: $ReadOnly, +): 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] diff --git a/packages/wonder-stuff-core/src/keys.js b/packages/wonder-stuff-core/src/keys.js index 201f616c..19a18423 100644 --- a/packages/wonder-stuff-core/src/keys.js +++ b/packages/wonder-stuff-core/src/keys.js @@ -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} obj The object for which the values are * to be returned. * @returns {Array<$Keys>} An array of the enumerable keys of an object. */ -export function keys(obj: $ReadOnly): Array<$Keys> { +export function keys( + obj: $ReadOnly, +): Array<$Keys> { return Object.keys(obj); } diff --git a/packages/wonder-stuff-core/src/values.js b/packages/wonder-stuff-core/src/values.js index 72f84b5c..73401dad 100644 --- a/packages/wonder-stuff-core/src/values.js +++ b/packages/wonder-stuff-core/src/values.js @@ -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} obj The object for which the values are * to be returned. * @returns {Array} An array of the enumerable property values of the object. */ -export function values(obj: $ReadOnly<{|[mixed]: V|}>): Array { +export function values(obj: $ReadOnly): Array { // This is a deliberate cast through any. // Object.values returns Array and we want to return Array. // $FlowIgnore[unclear-type]