-
-
Notifications
You must be signed in to change notification settings - Fork 141
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 P.object, P.object.empty and P.object.exact() #234
base: main
Are you sure you want to change the base?
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 |
TODO
P.object.exact({...})
P.object.exact({ prop: P.select() })
works, both for type inference and runtime.P.object
predicatesP.object
is itself a pattern, but also a module containing predicates related to object types.P.object
P.object
matches any value assignable to theobject
TypeScript type. This includes all object literals, but also arrays and functions!P.object
does 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: