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

Improve the hasProperty function #79

Merged
merged 2 commits into from
Feb 14, 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
16 changes: 15 additions & 1 deletion src/misc.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectAssignable, expectNotAssignable } from 'tsd';

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

//=============================================================================
// isObject
Expand Down Expand Up @@ -36,6 +36,20 @@ if (isObject(unknownObject)) {
expectNotAssignable<Record<'foo', unknown>>(unknownObject);
}

// An object is accepted after `hasProperty` is used to prove that it has the required property.
if (isObject(unknownObject) && hasProperty(unknownObject, 'foo')) {
expectAssignable<Record<'foo', unknown>>(unknownObject);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These two tests were failing before these changes

}

// An object is accepted after `hasProperty` is used to prove that it has all required properties.
if (
isObject(unknownObject) &&
hasProperty(unknownObject, 'foo') &&
hasProperty(unknownObject, 'bar')
) {
expectAssignable<Record<'foo' | 'bar', unknown>>(unknownObject);
}

// An object is not accepted after `hasProperty` has only been used to establish that some required properties exist.
if (isObject(unknownObject) && hasProperty(unknownObject, 'foo')) {
expectNotAssignable<Record<'foo' | 'bar', unknown>>(unknownObject);
Expand Down
5 changes: 1 addition & 4 deletions src/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ describe('miscellaneous', () => {
[
[{}, 'a'],
[{ a: 1 }, 'b'],
// Object.hasOwnProperty does not work for arrays
// [['foo'], 0],
// [['foo'], '0'],
] as any[]
] as const
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These array test cases didn't seem relevant because they contradicted the input type.

).forEach(([objectValue, property]) => {
expect(hasProperty(objectValue, property)).toBe(false);
});
Expand Down
21 changes: 12 additions & 9 deletions src/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ export type PartialOrAbsent<Value> = Partial<Value> | null | undefined;
export type NonEmptyArray<Element> = [Element, ...Element[]];

/**
* A JavaScript object that is not `null`, a function, or an array. The object
* can still be an instance of a class.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, this deletion was intended to be in #78. The tests added there show that instances of a class were not compatible with the RuntimeObject type.

* A JavaScript object that is not `null`, a function, or an array.
*/
export type RuntimeObject = Record<number | string | symbol, unknown>;
export type RuntimeObject = Record<PropertyKey, unknown>;

//
// Type Guards
Expand Down Expand Up @@ -80,17 +79,21 @@ export function isObject(value: unknown): value is RuntimeObject {
//

/**
* An alias for {@link Object.hasOwnProperty}.
* A type guard for ensuring an object has a property.
*
* @param object - The object to check.
* @param objectToCheck - The object to check.
* @param name - The property name to check for.
* @returns Whether the specified object has an own property with the specified
* name, regardless of whether it is enumerable or not.
*/
export const hasProperty = (
object: RuntimeObject,
name: string | number | symbol,
): boolean => Object.hasOwnProperty.call(object, name);
export const hasProperty = <
ObjectToCheck extends RuntimeObject,
Property extends PropertyKey,
>(
objectToCheck: ObjectToCheck,
name: Property,
): objectToCheck is ObjectToCheck & Record<Property, unknown> =>
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.hasOwnProperty.call(objectToCheck, name);

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

Expand Down