Skip to content

Commit

Permalink
[fixkeystypes] Improve keys typing (#321)
Browse files Browse the repository at this point in the history
## 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: #321
  • Loading branch information
somewhatabstract authored Jul 14, 2022
1 parent 99abd47 commit 1ec0968
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/polite-snails-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/wonder-stuff-core": patch
---

Update types for `keys` so that it works with a wider variety of object shapes
11 changes: 11 additions & 0 deletions packages/wonder-stuff-core/src/__tests__/keys.flowtest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion packages/wonder-stuff-core/src/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
* to be returned.
* @returns {Array<$Keys<O>>} An array of the enumerable keys of an object.
*/
export function keys<O: {[string]: mixed}>(obj: O): Array<$Keys<O>> {
export function keys<O: {[string]: mixed}>(obj: $ReadOnly<O>): Array<$Keys<O>> {
return Object.keys(obj);
}

0 comments on commit 1ec0968

Please sign in to comment.