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 hasProperty, hasDefinedProperty, hasValorizedProperty and PossibleKeys #191

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

raythurnvoid
Copy link

Description

Add 3 new objects related functions and 1 new utility type for union types:

hasProperty

Check if the object has the property, similar to the in operator 'key' in obj but unexistent properties are not allowed and it allows intellisense

Usage:

import { hasProperty } from 'utility-types';

if (hasProperty(obj, 'prop')) {
  // `prop` in `obj`
}

hasDefinedProperty

Check if the object has the property and it is not undefined

Usage:

import { hasDefinedProperty } from 'utility-types';

if (hasDefinedProperty(obj, 'prop')) {
  // `prop` in `obj` and `obj.prop` is not `undefined`
}

hasValorizedProperty

Check if the object has the property and it is not undefined and not null

Usage:

import { hasValorizedProperty } from 'utility-types';

if (hasValorizedProperty(obj, 'prop')) {
  // `prop` in `obj` and `obj.prop` is not `undefined` and not `null`
}

PossibleKeys<T>

Similar to $Keys or keyof, but get keys also from union types

Usage:

type Props = { name: string; employeeId: string } | { name: string; guestId: string };
// Expect: "name" | "employeeId" | "guestId"
type PropsKeys1 = PossibleKeys<Props>;
// Expect: "name"
type PropsKeys2 = $Keys<Props>;
// Expect: "name"
type PropsKeys3 = keyof Props;

Related issues:

Checklist

  • I have read CONTRIBUTING.md
  • I have linked all related issues above
  • I have rebased my branch

For bugfixes:

  • I have added at least one unit test to confirm the bug have been fixed
  • I have checked and updated TOC and API Docs when necessary

For new features:

  • I have added entry in TOC and API Docs
  • I have added a short example in API Docs to demonstrate new usage
  • I have added type unit tests with dts-jest

@raythurnvoid raythurnvoid changed the title add 3 hasProperty, hasDefinedProperty, hasValorizedProperty and PossibleKeys add hasProperty, hasDefinedProperty, hasValorizedProperty and PossibleKeys Jan 28, 2024
Copy link
Owner

@piotrwitek piotrwitek left a comment

Choose a reason for hiding this comment

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

Hey @raythurnevoid thanks a lot for your contribution. It looks amazing.
I have a couple of suggestions regarding the naming conventions of this project to keep consistency :)
If you could apply comments I would be more than happy to merge and release :)
Optionally you could put yourself or sources in the jsdocs credits :)
Thank you :)

@@ -226,6 +230,48 @@ const consumer = (param: Nullish | string): string => {
};
```

### `hasProperty`
Copy link
Owner

Choose a reason for hiding this comment

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

looks great

}
```

### `hasValorizedProperty`
Copy link
Owner

Choose a reason for hiding this comment

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

I'm gravitating towadrs hasNonEmptyProperty WDYT?

Comment on lines +471 to +488
### `PossibleKeys<T>`

Similar to [`$Keys`](#keyst) or `keyof`, but get keys also from union types

**Usage:**

```ts
type Props = { name: string; employeeId: string } | { name: string; guestId: string };
// Expect: "name" | "employeeId" | "guestId"
type PropsKeys1 = PossibleKeys<Props>;
// Expect: "name"
type PropsKeys2 = $Keys<Props>;
// Expect: "name"
type PropsKeys3 = keyof Props;
```

[⇧ back to top](#table-of-contents)

Copy link
Owner

Choose a reason for hiding this comment

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

we already have another proposal for this type called UnionKeys.
I layed out specs there which are not respected here so I propose either:

  • update this PR according to specs
  • remove this type from this PR

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.

add "has_property" and "has_value" type guards for objects
2 participants