diff --git a/.changeset/stupid-beds-walk.md b/.changeset/stupid-beds-walk.md new file mode 100644 index 00000000..dfef8de5 --- /dev/null +++ b/.changeset/stupid-beds-walk.md @@ -0,0 +1,5 @@ +--- +"@khanacademy/wonder-stuff-core": patch +--- + +Fix the typing for the `key` function diff --git a/packages/wonder-stuff-core/src/__tests__/keys.typestest.ts b/packages/wonder-stuff-core/src/__tests__/keys.typestest.ts index 75969492..3f1b72b1 100644 --- a/packages/wonder-stuff-core/src/__tests__/keys.typestest.ts +++ b/packages/wonder-stuff-core/src/__tests__/keys.typestest.ts @@ -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. 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. diff --git a/packages/wonder-stuff-core/src/keys.ts b/packages/wonder-stuff-core/src/keys.ts index 89777a4d..0831a756 100644 --- a/packages/wonder-stuff-core/src/keys.ts +++ b/packages/wonder-stuff-core/src/keys.ts @@ -1,12 +1,11 @@ /** * Return an array of the enumerable keys of an object. * - * @param {$ReadOnly} obj The object for which the values are + * @param {object} obj The object for which the values are * to be returned. * @returns {Array<$Keys>} 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(obj: O): Array { + return Object.keys(obj) as Array; }