-
-
Notifications
You must be signed in to change notification settings - Fork 10
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
* 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 | ||
|
@@ -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> => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>; | ||
|
||
|
There was a problem hiding this comment.
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