Skip to content

Commit

Permalink
feat(types): Tryit
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <[email protected]>
  • Loading branch information
unicornware committed May 22, 2023
1 parent e30eb30 commit cee68ef
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 2 deletions.
1 change: 1 addition & 0 deletions .dictionary.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pkgs
preid
promisable
stringafiable
tryit
tscu
unstub
vates
Expand Down
20 changes: 20 additions & 0 deletions src/types/__tests__/tryit.spec-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* @file Type Tests - Tryit
* @module tutils/types/tests/unit-d/Tryit
*/

import type Book from '#fixtures/book.interface'
import type NumberString from '../number-string'
import type TestSubject from '../tryit'

describe('unit-d:types/Tryit', () => {
it('should convert T to error-first callback', () => {
// Arrange
type T = (id: NumberString) => Promise<Book>
type R = Promise<[Error, null] | [null, Awaited<ReturnType<T>>]>

// Expect
expectTypeOf<TestSubject<T>>().parameters.toEqualTypeOf<Parameters<T>>
expectTypeOf<TestSubject<T>>().returns.toEqualTypeOf<R>
})
})
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ export type { default as TimestampFormat } from './timestamp-format'
export type { default as Trim } from './trim'
export type { default as TrimEnd } from './trim-end'
export type { default as TrimStart } from './trim-start'
export type { default as Tryit } from './tryit'
export type { default as TupleLength } from './tuple-length'
export type { default as TupleToUnion } from './tuple-to-union'
export type { default as TypedArray } from './typed-array'
Expand Down
4 changes: 2 additions & 2 deletions src/types/promisable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
*/

/**
* Type representing an awaitable or synchronous value.
* An awaitable or synchronous value.
*
* @see https://github.com/microsoft/TypeScript/issues/31394
*
* @template T - Value type
* @template T - Type to evalute
*/
type Promisable<T> = PromiseLike<T> | T

Expand Down
19 changes: 19 additions & 0 deletions src/types/tryit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @file Type Definitions - Tryit
* @module tutils/types/Tryit
*/

import type Fn from './fn'

/**
* Converts `T` to an error-first callback.
*
* @template T - Function to convert
* @template E - Error type
*/
type Tryit<T extends Fn, E extends Error = Error> = Fn<
Parameters<T>,
Promise<[E, null] | [null, Awaited<ReturnType<T>>]>
>

export type { Tryit as default }

0 comments on commit cee68ef

Please sign in to comment.