From 1ec0968a8f25daca40092f8533421584ebc7df02 Mon Sep 17 00:00:00 2001 From: Jeff Yates Date: Thu, 14 Jul 2022 12:22:05 -0500 Subject: [PATCH] [fixkeystypes] Improve keys typing (#321) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary: I found that the typing was not helpful so I added a test case for the issue I experienced and then tweaked the function definition so that all the flow tests passed. No functional change here, purely types. Issue: FEI-4655 ## Test plan: `yarn flow` Author: somewhatabstract Reviewers: jeresig, benchristel, somewhatabstract Required Reviewers: Approved By: jeresig Checks: ✅ Test (macOS-latest, 16.x), ✅ codecov/project, ✅ CodeQL, ✅ Lint, flow, and coverage check (ubuntu-latest, 16.x), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 16.x), ✅ gerald, ✅ Analyze (javascript), ⏭ dependabot Pull Request URL: https://github.com/Khan/wonder-stuff/pull/321 --- .changeset/polite-snails-grab.md | 5 +++++ .../wonder-stuff-core/src/__tests__/keys.flowtest.js | 11 +++++++++++ packages/wonder-stuff-core/src/keys.js | 2 +- 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 .changeset/polite-snails-grab.md diff --git a/.changeset/polite-snails-grab.md b/.changeset/polite-snails-grab.md new file mode 100644 index 00000000..4bffe55c --- /dev/null +++ b/.changeset/polite-snails-grab.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/wonder-stuff-core": patch +--- + +Update types for `keys` so that it works with a wider variety of object shapes diff --git a/packages/wonder-stuff-core/src/__tests__/keys.flowtest.js b/packages/wonder-stuff-core/src/__tests__/keys.flowtest.js index 9833bc51..accb40f6 100644 --- a/packages/wonder-stuff-core/src/__tests__/keys.flowtest.js +++ b/packages/wonder-stuff-core/src/__tests__/keys.flowtest.js @@ -32,3 +32,14 @@ import {keys} from "../keys.js"; const keys2bad = keys(obj2); const __: "a" = keys2bad[0]; } + +{ + // should work with more specific object types + const obj3: {|[string]: string|} = { + a: "1", + b: "2", + }; + + // This should not be erroring. + const _ = keys(obj3); +} diff --git a/packages/wonder-stuff-core/src/keys.js b/packages/wonder-stuff-core/src/keys.js index e2b01164..201f616c 100644 --- a/packages/wonder-stuff-core/src/keys.js +++ b/packages/wonder-stuff-core/src/keys.js @@ -6,6 +6,6 @@ * to be returned. * @returns {Array<$Keys>} An array of the enumerable keys of an object. */ -export function keys(obj: O): Array<$Keys> { +export function keys(obj: $ReadOnly): Array<$Keys> { return Object.keys(obj); }