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

Improve the hasProperty function #79

merged 2 commits into from
Feb 14, 2023

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Feb 10, 2023

The hasProperty function now tells TypeScript that the property exists, making it more effective as a type guard.

A ValidPropertyTypes type has been added as well, for convenience.

@Gudahtt

This comment was marked as resolved.

@@ -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

>(
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.

// [['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.

Base automatically changed from add-misc-type-tests to main February 10, 2023 19:58
The `hasProperty` function now tells TypeScript that the property
exists, making it more effective as a type guard.

A `ValidPropertyTypes` type has been added as well, for convenience.
@Gudahtt Gudahtt marked this pull request as ready for review February 10, 2023 19:59
@Gudahtt Gudahtt requested a review from a team as a code owner February 10, 2023 19:59
@@ -32,10 +32,14 @@ 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.

src/misc.ts Outdated Show resolved Hide resolved
Copy link
Member

@Mrtenz Mrtenz left a comment

Choose a reason for hiding this comment

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

This is great. 👍

@Gudahtt Gudahtt merged commit 8d13a40 into main Feb 14, 2023
@Gudahtt Gudahtt deleted the improve-has-property branch February 14, 2023 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants