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

feat: adds has module #137

Merged
merged 3 commits into from
May 24, 2024
Merged

feat: adds has module #137

merged 3 commits into from
May 24, 2024

Conversation

ahrjarrett
Copy link
Owner

@ahrjarrett ahrjarrett commented May 24, 2024

changelog

new module: has

  • has.oneProperty

    • has.oneProperty lets you express the type of an object that contains exactly 1 property.

    For example:

    /** 
     * @example
     *  const one = singleton({ a: 1 })       // ✅
     * 
     *  const zero = singleton({})            // 🚫
     *  //    ^? const zero: never
     * 
     *  const two = singleton({ a: 1, b: 2 }) // 🚫
     *  //    ^? const two: never
     */
    declare function singleton<const T extends has.oneProperty<T>>(
      objectWithExactlyOneProperty: T
    ): T
  • has.oneProperty.debug

    • has.oneProperty.debug is like has.oneProperty, but raises a TypeError if T has 0 or 2+ properties
  • has.oneElement

    • has.oneElement lets you express the type of an tuple that contains exactly 1 property.

    For example:

    /** 
     * @example
     *  const one = singleton([1])    // ✅
     *  //    ^? const one: readonly [1]
     * 
     *  const zero = singleton([])    // 🚫
     *  //    ^? const zero: never
     *  const two = singleton([1, 2]) // 🚫
     *  //    ^? const two: never
     */
    declare function oneNumber<const T extends has.oneElement<T, number>>(
      tupleContainingOneNumber: T
    ): T
  • has.oneElement.debug

    • has.oneElement.debug is like has.oneElement, but raises a TypeError if T has 0 or 2+ elements

Notes

  • Both has.oneProperty and has.oneElement both accept 2 additional optional type parameters:
    1. an invariant param, which allows you to apply an additional constraint on the type of the property itself; and
    2. a debug flag, which, if non-never, changes the behavior of has.one* to raise a TypeError if T fails to satisfy the constraint

Copy link

changeset-bot bot commented May 24, 2024

🦋 Changeset detected

Latest commit: 0dbbab0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
any-ts Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@ahrjarrett ahrjarrett merged commit 3fe5ee7 into main May 24, 2024
1 check passed
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.

1 participant