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 P.object, P.object.empty and P.object.exact() #234

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

gvergnaud
Copy link
Owner

@gvergnaud gvergnaud commented Mar 31, 2024

TODO

  • add unit tests and doc for P.object.exact({...})
  • Make sure P.object.exact({ prop: P.select() }) works, both for type inference and runtime.

P.object predicates

P.object is itself a pattern, but also a module containing predicates related to object types.

P.object

P.object matches any value assignable to the object TypeScript type. This includes all object literals, but also arrays and functions!

P.object does not match primitive types, like strings or numbers.

import { match, P } from 'ts-pattern';

const fn = (input: unknown) =>
  match(input)
    .with(P.object, () => '✅')
    .otherwise(() => '❌');

console.log(fn({})); // ✅
console.log(fn({ hello: 'world!' })); // ✅
console.log(fn([])); // ✅
console.log(fn(() => {})); // ✅

console.log(fn(1, true, 'hi')); // ❌ ❌ ❌

P.object.empty()

P.object.empty() matches the empty object {}:

import { isMatching, P } from 'ts-pattern';

console.log(isMatching(P.object.empty(), {})); // true

P.object.empty() does not match empty arrays, 0 values or nullish values:

import { isMatching, P } from 'ts-pattern';

console.log(isMatching(P.object.empty(), [])); // false
console.log(isMatching(P.object.empty(), 0)); // false
console.log(isMatching(P.object.empty(), null)); // false
console.log(isMatching(P.object.empty(), undefined)); // false

@gvergnaud gvergnaud changed the title Add P.object and P.object.empty Add P.object, P.object.empty and P.object.exact() Mar 31, 2024
gitsunmin and others added 6 commits March 31, 2024 16:41
- 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?
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.

None yet

2 participants