Skip to content

Commit

Permalink
feat(types): ArrayFallback
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed Aug 13, 2023
1 parent 1fa5c84 commit 95d4115
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/types/__tests__/fallback-array.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @file Type Tests - ArrayFallback
* @module tutils/types/tests/unit-d/ArrayFallback
*/

import type Vehicle from '#fixtures/types/vehicle'
import type EmptyArray from '../empty-array'
import type TestSubject from '../fallback-array'
import type Nilable from '../nilable'
import type Partial from '../partial'

describe('unit-d:types/ArrayFallback', () => {
it('should equal F if T extends NIL', () => {
// Arrange
type F = [Partial<Vehicle>]

// Expect
expectTypeOf<TestSubject<null, F>>().toEqualTypeOf<F>()
expectTypeOf<TestSubject<undefined, F>>().toEqualTypeOf<F>()
})

it('should equal T if IsAny<T> extends true', () => {
// Arrange
type T = any

// Expect
expectTypeOf<TestSubject<T>>().toEqualTypeOf<T>()
})

it('should equal T if T extends {}', () => {
// Arrange
type T = Vehicle

// Expect
expectTypeOf<TestSubject<T>>().toEqualTypeOf<T>()
})

describe('unions', () => {
it('should distribute over unions', () => {
// Arrange
type T = Nilable<readonly [Vehicle, Vehicle]>
type Expect = EmptyArray | NonNullable<T>

// Expect
expectTypeOf<TestSubject<T>>().toEqualTypeOf<Expect>()
})
})
})
24 changes: 24 additions & 0 deletions src/types/fallback-array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* @file Type Definitions - ArrayFallback
* @module tutils/types/ArrayFallback
*/

import type EmptyArray from './empty-array'
import type Fallback from './fallback'
import type NIL from './nil'

/**
* Fallback to an array type if `T` extends {@linkcode NIL}.
*
* @todo examples
*
* @template T - Type to evaluate
* @template [F=EmptyArray] - Fallback array type
*/
type ArrayFallback<T, F extends readonly unknown[] = EmptyArray> = Fallback<
T,
F,
NIL
>

export type { ArrayFallback as default }
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type { default as EmptyValue } from './empty-value'
export type { default as Entries } from './entries'
export type { default as ExactOptionalPropertyTypes } from './exact-optional-property-types'
export type { default as Fallback } from './fallback'
export type { default as ArrayFallback } from './fallback-array'
export type { default as Falsy } from './falsy'
export type { default as FIXME } from './fixme'
export type { default as Flat } from './flat'
Expand Down

0 comments on commit 95d4115

Please sign in to comment.