From 753b1939bf7cb1fbd62d0273a9a8703527225b2d Mon Sep 17 00:00:00 2001 From: Jeff Yates Date: Thu, 14 Jul 2022 15:01:21 -0500 Subject: [PATCH] [objasinterface] Make sure that keys/values/entries are typed to work against class instances too (#329) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary: Noticed in webapp that some uses are passing a class instance and we should support that. Flow error indicated using an interface rather than object type so that it would match both objects and class instances. So this does that. Issue: XXX-XXXX ## Test plan: `yarn flow` Author: somewhatabstract Reviewers: kevinbarabash, benchristel Required Reviewers: Approved By: kevinbarabash Checks: ✅ codecov/project, ✅ Test (macOS-latest, 16.x), ✅ CodeQL, ✅ Lint, flow, and coverage check (ubuntu-latest, 16.x), ⏭ dependabot, ✅ Analyze (javascript), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 16.x), ✅ gerald Pull Request URL: https://github.com/Khan/wonder-stuff/pull/329 --- .changeset/nice-shirts-switch.md | 5 +++++ .../src/__tests__/entries.flowtest.js | 12 ++++++++++++ .../wonder-stuff-core/src/__tests__/keys.flowtest.js | 12 ++++++++++++ .../src/__tests__/values.flowtest.js | 12 ++++++++++++ packages/wonder-stuff-core/src/entries.js | 6 ++++-- packages/wonder-stuff-core/src/keys.js | 6 ++++-- packages/wonder-stuff-core/src/values.js | 4 ++-- 7 files changed, 51 insertions(+), 6 deletions(-) create mode 100644 .changeset/nice-shirts-switch.md 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]