-
-
Notifications
You must be signed in to change notification settings - Fork 229
add hasProperty, hasDefinedProperty, hasValorizedProperty and PossibleKeys #191
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
base: master
Are you sure you want to change the base?
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 |
---|---|---|
|
@@ -90,6 +90,9 @@ We are open for contributions. If you're planning to contribute please make sure | |
* [`isFalsy`](#isfalsy) | ||
* [`Nullish`](#nullish) | ||
* [`isNullish`](#isnullish) | ||
* [`hasProperty`](#hasproperty) | ||
* [`hasDefinedProperty`](#hasdefinedproperty) | ||
* [`hasValorizedProperty`](#hasvalorizedproperty) | ||
|
||
## Union operators | ||
|
||
|
@@ -130,6 +133,7 @@ We are open for contributions. If you're planning to contribute please make sure | |
* [`Overwrite<T, U>`](#overwritet-u) | ||
* [`Assign<T, U>`](#assignt-u) | ||
* [`ValuesType<T>`](#valuestypet) | ||
* [`PossibleKeys<T>`](#possiblekeyst) | ||
|
||
## Special operators | ||
|
||
|
@@ -226,6 +230,48 @@ const consumer = (param: Nullish | string): string => { | |
}; | ||
``` | ||
|
||
### `hasProperty` | ||
|
||
Check if the object has the property, similar to [the `in` operator](https://www.typescriptlang.org/docs/handbook/2/narrowing.html#the-in-operator-narrowing) `'key' in obj` but unexistent properties are not allowed and it allows intellisense | ||
|
||
**Usage:** | ||
|
||
```ts | ||
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:** | ||
|
||
```ts | ||
import { hasDefinedProperty } from 'utility-types'; | ||
|
||
if (hasDefinedProperty(obj, 'prop')) { | ||
// `prop` in `obj` and `obj.prop` is not `undefined` | ||
} | ||
``` | ||
|
||
### `hasValorizedProperty` | ||
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. I'm gravitating towadrs |
||
|
||
Check if the object has the property and it is not `undefined` and not `null` | ||
|
||
**Usage:** | ||
|
||
```ts | ||
import { hasValorizedProperty } from 'utility-types'; | ||
|
||
if (hasValorizedProperty(obj, 'prop')) { | ||
// `prop` in `obj` and `obj.prop` is not `undefined` and not `null` | ||
} | ||
``` | ||
|
||
[⇧ back to top](#table-of-contents) | ||
|
||
### `SetIntersection<A, B>` (same as Extract) | ||
|
@@ -422,6 +468,24 @@ type Keys = OptionalKeys<Props>; | |
|
||
[⇧ back to top](#table-of-contents) | ||
|
||
### `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) | ||
|
||
Comment on lines
+471
to
+488
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. we already have another proposal for this type called UnionKeys.
|
||
### `Optional<T, K>` | ||
|
||
From `T` make a set of properties by key `K` become optional | ||
|
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.
looks great