Skip to content

Commit

Permalink
[🔥AUDIT🔥] [rg.audit.3] Fix keys typing (#620)
Browse files Browse the repository at this point in the history
🖍 _This is an audit!_ 🖍

## Summary:
This updates the typing of the `keys` function to match better with our expected usage.

Issue: FEI-4960

## Test plan:
`yarn typecheck`

Also built the types and tried them in webapp to verify they now work as we would want.

Author: somewhatabstract

Auditors: #typescript, #frontend-infra

Required Reviewers:

Approved By:

Checks: ⏭  dependabot, ⌛ gerald, ⌛ Prime node_modules cache for primary configuration (ubuntu-latest, 16.x), ⌛ Analyze (javascript)

Pull Request URL: #620
  • Loading branch information
somewhatabstract authored Apr 3, 2023
1 parent d0c21c7 commit b28725a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/stupid-beds-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/wonder-stuff-core": patch
---

Fix the typing for the `key` function
3 changes: 0 additions & 3 deletions packages/wonder-stuff-core/src/__tests__/keys.typestest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ import {keys} from "../keys";
c: [3, 4],
} as const;

// It would be nice if this worked, but TypeScript's library definition
// defines the return type of Object.keys() to be Array<string>.
const keys2bad = keys(obj2);
// @ts-expect-error: string is not assignable to "a" | "b" | "c"
const _: "a" | "b" | "c" = keys2bad[0];

// @ts-expect-error: This errors because we try to get a key of only one type.
Expand Down
7 changes: 3 additions & 4 deletions packages/wonder-stuff-core/src/keys.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
/**
* Return an array of the enumerable keys of an object.
*
* @param {$ReadOnly<interface {[string]: mixed}>} obj The object for which the values are
* @param {object} obj The object for which the values are
* to be returned.
* @returns {Array<$Keys<O>>} An array of the enumerable keys of an object.
*/
// NOTE(kevinb): This type was copied from TypeScript's library definitions.
// eslint-disable-next-line @typescript-eslint/ban-types
export function keys(obj: {}): string[] {
return Object.keys(obj);
export function keys<O extends object>(obj: O): Array<keyof O> {
return Object.keys(obj) as Array<keyof O>;
}

0 comments on commit b28725a

Please sign in to comment.