Skip to content

Commit

Permalink
Fix support for interface param in objectKeys (#51)
Browse files Browse the repository at this point in the history
Closes #50
  • Loading branch information
ifiokjr authored Apr 12, 2022
1 parent 88ada60 commit f330745
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 4 additions & 2 deletions source/object-keys.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export type ObjectKeys<T extends Record<PropertyKey, unknown>> = `${Exclude<keyof T, symbol>}`;
/* eslint-disable @typescript-eslint/ban-types */

export type ObjectKeys<T extends object> = `${Exclude<keyof T, symbol>}`;

/**
A strongly-typed version of `Object.keys()`.
Expand All @@ -19,6 +21,6 @@ const untypedItems = Object.keys(items); // => Array<string>
@category Improved builtin
@category Type guard
*/
export function objectKeys<Type extends Record<PropertyKey, unknown>>(value: Type): Array<ObjectKeys<Type>> {
export function objectKeys<Type extends object>(value: Type): Array<ObjectKeys<Type>> {
return Object.keys(value) as Array<ObjectKeys<Type>>;
}
11 changes: 10 additions & 1 deletion test/object-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@ import test from 'ava';
import {expectTypeOf} from 'expect-type';
import {objectKeys} from '../source/index.js';

interface TestInterface {
e: string;
f: number;
}

test('objectKeys()', t => {
type Item = 'a' | 'b' | 'c' | '4';
const items = objectKeys({a: 1, b: 2, c: 3, 4: 4, [Symbol('5')]: 5});

expectTypeOf<Item[]>(items);
t.deepEqual(items, ['4', 'a', 'b', 'c']);

const interfaceInput: TestInterface = {e: 'a', f: 1};
const interfaceItems = objectKeys(interfaceInput);
expectTypeOf<Array<keyof TestInterface>>(interfaceItems);
t.deepEqual(interfaceItems, ['e', 'f']);
});

0 comments on commit f330745

Please sign in to comment.