Skip to content

Commit

Permalink
feat(types): Shake
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Jul 25, 2023
1 parent eade439 commit 435a9c4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/types/__tests__/shake.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @file Type Tests - Shake
* @module tutils/types/tests/unit-d/Shake
*/

import type Vehicle from '#fixtures/types/vehicle'
import type Fn from '../fn'
import type NumberString from '../number-string'
import type Optional from '../optional'
import type Partial from '../partial'
import type TestSubject from '../shake'

describe('unit-d:types/Shake', () => {
it('should equal {} if F is any', () => {
expectTypeOf<TestSubject<Vehicle, any>>().toEqualTypeOf<{}>()
})

it('should equal T if F is never', () => {
expectTypeOf<TestSubject<Vehicle, never>>().toEqualTypeOf<Vehicle>()
})

describe('T extends ObjectCurly', () => {
it('should exclude F from T', () => {
// Arrange
type T = { vin: string; year: NumberString }
type F = string

// Expect
expectTypeOf<TestSubject<T, F>>().toEqualTypeOf<{
year: Exclude<NumberString, F>
}>()
})
})

describe('unions', () => {
it('should distribute over unions', () => {
// Arrange
type T = Partial<Vehicle> | Vehicle
type F = Optional<Fn | string>

// Expect
expectTypeOf<TestSubject<T, F>>().toEqualTypeOf<
{ year: Vehicle['year'] } | { year?: Vehicle['year'] }
>()
})
})
})
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export type { default as NaturalRange } from './range-natural'
export type { default as Remap } from './remap'
export type { default as Reverse } from './reverse'
export type { default as Segment } from './segment'
export type { default as Shake } from './shake'
export type { default as Sift } from './sift'
export type { default as Simplify } from './simplify'
export type { default as Split } from './split'
Expand Down
23 changes: 23 additions & 0 deletions src/types/shake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @file Type Definitions - Shake
* @module tutils/types/Shake
*/

import type ObjectCurly from './object-curly'

/**
* Filter `T` by removing keys where the key-value non-distributively extends
* filter condition `F`, and exclude `F` from any remaining key values.
*
* **Note**: Does **not** make optional keys required.
*
* @todo examples
*
* @template T - Type to evaluate
* @template F - Key value filter
*/
type Shake<T extends ObjectCurly, F = undefined> = T extends unknown
? { [K in keyof T as T[K] extends F ? never : K]: Exclude<T[K], F> }
: never

export type { Shake as default }

0 comments on commit 435a9c4

Please sign in to comment.