Skip to content

Commit

Permalink
feat: add extractFunction()
Browse files Browse the repository at this point in the history
  • Loading branch information
unional committed Nov 18, 2022
1 parent 1d4d63b commit 1192e49
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ type Yes = IsAny<any, 'yes', 'no'> // 'yes'
type No = IsAny<1, 'yes', 'no'> // 'no'
```
### Array function
### Array Utilities
- `CommonPropKeys<A>`: gets common keys inside the records in the array `A` (deprecate `CommonKeys`).
- `Concat<A, B>`: `[...A, ...B]`.
Expand Down Expand Up @@ -552,10 +552,11 @@ const b2 = brand('y', 1)
nominalMatch(b1, b2) // false
```

## Function Types
## Function Utilties

- `AnyFunction<P, R>`: a generic type for any function
- `ExtractFunction<F>`: extract the function signature from a type `F`.
- `extractFunction(fn: F)`: adjust type of `fn` to its function signature only.

## Functional Types

Expand Down
12 changes: 11 additions & 1 deletion ts/function/ExtractFunction.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExtractFunction, isType } from '../index.js'
import { ExtractFunction, extractFunction, isType } from '../index.js'

it('gets the function itself if it is not composite', () => {
type F = () => void
Expand All @@ -13,3 +13,13 @@ it('omits other props from the type', () => {

isType.equal<true, () => void, R>()
})

describe(`${extractFunction.name}()`, () => {
it('adjust type to its function type', () => {
const fn = Object.assign(() => { }, { a: 1 })

const r = extractFunction(fn)
type R = typeof r
isType.equal<true, () => void, R>()
})
})
4 changes: 4 additions & 0 deletions ts/function/ExtractFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ import { AnyFunction } from './AnyFunction.js'
*/
export type ExtractFunction<T extends AnyFunction> = T extends AnyFunction<infer P, infer R>
? (...args: P) => R : never

export function extractFunction<T extends AnyFunction>(fn: T) {
return fn as any as ExtractFunction<T>
}
2 changes: 1 addition & 1 deletion ts/function/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export type { AnyFunction } from './AnyFunction.js'
export type { ExtractFunction } from './ExtractFunction.js'
export * from './ExtractFunction.js'
2 changes: 1 addition & 1 deletion ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export * from './array/index.js'
export * from './assertion/index.js'
export * from './class/index.js'
export type { ComposableTypes, NonComposableTypes } from './ComposableTypes.js'
export type { AnyFunction, ExtractFunction } from './function/index.js'
export * from './function/index.js'
export * from './functional/index.js'
export type { JSONArray, JSONObject, JSONPrimitive, JSONTypes } from './JSONTypes.js'
export type { Abs, Add, Decrement, Digit, DigitArray, GreaterThan, Increment, IsPositive, IsWhole, Max, Subtract } from './math/index.js'
Expand Down

0 comments on commit 1192e49

Please sign in to comment.