Add P.object, P.object.empty and P.object.exact()#234
Open
Add P.object, P.object.empty and P.object.exact()#234
Conversation
- Comment: Seems like chainable would be enough here, since you can't chain empty several times - Comment: I think we could make this more efficient by using a for in loop instead of Object.keys and breaking the loop by returning false if an object own property is encountered. - Comment: It should just be Chainable here as well - Comment: I'm not sure a new pattern type is necessary here because both patterns you added are implemented with guards - Comment: Could you add test covering how P.object behaves with more inputs: Functions, Primitive values, Null. It should catch all values that are assignable to the object type, and type narrowing and exhaustive should both work - Comment: Could you remove this diff?
fb3c6fb to
6cc4989
Compare
|
Very nice! Looking forward to using this new pattern |
|
Any plan for this PR? |
|
I know this MR has been open for a while, but I was wondering - would users of this pattern expect /** Check if the value provided is a key-value object (Record, not array, string, etc.) */
export const isRecord = (
value: unknown,
): value is Record<string | number | symbol, unknown> => {
return Boolean(value?.constructor.name === 'Object');
};Here, arrays and nulls return |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TODO
P.object.exact({...})P.object.exact({ prop: P.select() })works, both for type inference and runtime.P.objectpredicatesP.objectis itself a pattern, but also a module containing predicates related to object types.P.objectP.objectmatches any value assignable to theobjectTypeScript type. This includes all object literals, but also arrays and functions!P.objectdoes not match primitive types, like strings or numbers.P.object.empty()P.object.empty()matches the empty object{}:P.object.empty()does not match empty arrays, 0 values or nullish values: