Skip to content

Improve the hasProperty function#79

Merged
Gudahtt merged 2 commits intomainfrom
improve-has-property
Feb 14, 2023
Merged

Improve the hasProperty function#79
Gudahtt merged 2 commits intomainfrom
improve-has-property

Conversation

@Gudahtt
Copy link
Copy Markdown
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.


// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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 force-pushed the improve-has-property branch from c2670bf to dd1bcc4 Compare February 10, 2023 19:58
@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

/**
* A JavaScript object that is not `null`, a function, or an array. The object
* can still be an instance of a class.
Copy link
Copy Markdown
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.

Copy link
Copy Markdown
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