Skip to content

Commit

Permalink
Merge pull request #56 from cuppachino/require-some
Browse files Browse the repository at this point in the history
✨add `RequireSome` type
  • Loading branch information
cuppachino authored Jul 28, 2023
2 parents c4cb6dd + 1df07fa commit e8d6372
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-falcons-rest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@cuppachino/type-space": patch
---

✨ Feat `RequireSome` type
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ The source code is fully tsdoc'd, so you can use your IDE's intellisense to refe
- [`Mutable`](src/mutable.ts): Recursively removes the `readonly` modifier from all properties of a type.
- [`PartialSome`](src/partial-some.ts): Return a new type that allows the specified keys to be undefined.
- [`PickAll`](src/pick-all.ts): Extract properties from _all_ members in a union, missing properties default to `| undefined`.
- [`RequireSome`](src/require-some.ts): Return a new type requiring the selected keys.
- [`Simplify`](src/simplify.ts): Simplify a type by mapping over its inferred properties - use when `Combine` cannot infer a deep type.
- [`Subset`](src/subset.ts): TypeScript equivalent of ``.
- [`UnionLiteral`](src/union-literal.ts): Create a union from a literal and primitive type without losing the literal type.
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type { Mutable } from 'type-space/mutable'
export type { ParseNumberLiteral } from 'type-space/parse-number-literal'
export type { PartialSome } from 'type-space/partial-some'
export type { PickAll } from 'type-space/pick-all'
export type { RequireSome } from 'type-space/require-some'
export type { Zip } from 'type-space/zip'

// strings
Expand Down
22 changes: 22 additions & 0 deletions src/require-some.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Combine } from './combine'

/**
* Returns a new type where some properties `K` of `T` are required.
* The rest are left as-is.
*
* @example
* ```
* type Data = {
* a?: string;
* b?: string;
* };
* type Ex = RequireSome<Test, "a">;
* ```
*/
export type RequireSome<T, K extends keyof T> = Combine<
{
[P in keyof T as Extract<P, K>]-?: T[P]
} & {
[P in keyof T as Exclude<P, K>]: T[P]
}
>

0 comments on commit e8d6372

Please sign in to comment.