Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getKnownPropertyNames #111

Merged
merged 4 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/misc.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { expectAssignable, expectNotAssignable, expectType } from 'tsd';

import { isObject, hasProperty, RuntimeObject } from './misc';
import {
isObject,
hasProperty,
getKnownPropertyNames,
RuntimeObject,
} from './misc';

//=============================================================================
// isObject
Expand Down Expand Up @@ -99,6 +104,19 @@ if (hasProperty(hasPropertyTypeExample, 'a')) {
expectType<number | undefined>(hasPropertyTypeExample.a);
}

//=============================================================================
// getKnownPropertyNames
//=============================================================================

enum GetKnownPropertyNamesEnumExample {
Foo = 'bar',
Baz = 'qux',
}

expectType<('Foo' | 'Baz')[]>(
getKnownPropertyNames(GetKnownPropertyNamesEnumExample),
);

//=============================================================================
// RuntimeObject
//=============================================================================
Expand Down
16 changes: 16 additions & 0 deletions src/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
isNullOrUndefined,
isObject,
hasProperty,
getKnownPropertyNames,
RuntimeObject,
isPlainObject,
calculateNumberSize,
Expand Down Expand Up @@ -116,6 +117,21 @@ describe('miscellaneous', () => {
});
});

describe('getKnownPropertyNames', () => {
it('returns the own property names of the object', () => {
const object = { foo: 'bar', baz: 'qux' };

expect(getKnownPropertyNames(object)).toStrictEqual(['foo', 'baz']);
});

it('does not return inherited properties', () => {
const superObject = { foo: 'bar' };
const object = Object.create(superObject);

expect(getKnownPropertyNames(object)).toStrictEqual([]);
});
});

describe('isPlainObject', () => {
it('should return true for a plain object', () => {
const somePlainObject = {
Expand Down
17 changes: 17 additions & 0 deletions src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,23 @@ export const hasProperty = <
Property extends keyof ObjectToCheck ? ObjectToCheck[Property] : unknown
> => Object.hasOwnProperty.call(objectToCheck, name);

/**
* `Object.getOwnPropertyNames()` is intentionally generic: it returns the
* immediate property names of an object, but it cannot make guarantees about
* the contents of that object, so the type of the property names is merely
* `string[]`. While this is technically accurate, it is also unnecessary if we
* have an object with a type that we own (such as an enum).
*
* @param object - The plain object.
* @returns The own property names of the object which are assigned a type
* derived from the object itself.
*/
export function getKnownPropertyNames<Key extends PropertyKey>(
object: Partial<Record<Key, any>>,
): Key[] {
return Object.getOwnPropertyNames(object) as Key[];
}

export type PlainObject = Record<number | string | symbol, unknown>;

/**
Expand Down