functions: add MapReturnType and MapParameters
This commit is contained in:
@@ -138,6 +138,8 @@ type res5 = Pipe<
|
|||||||
- [x] `ReturnType<Fn>`
|
- [x] `ReturnType<Fn>`
|
||||||
- [x] `Parameters<Fn>`
|
- [x] `Parameters<Fn>`
|
||||||
- [x] `Parameter<N, Fn>`
|
- [x] `Parameter<N, Fn>`
|
||||||
|
- [x] `MapReturnType<Fn, FunctionType>`
|
||||||
|
- [x] `MapParameters<Fn, FunctionType>`
|
||||||
- [ ] Tuples
|
- [ ] Tuples
|
||||||
- [x] `Create<X> -> [X]`
|
- [x] `Create<X> -> [X]`
|
||||||
- [x] `Partition<Tuple>`
|
- [x] `Partition<Tuple>`
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Fn, PartialApply, unset, _ } from "../core/Core";
|
import { Fn, PartialApply, unset, _, Call } from "../core/Core";
|
||||||
|
|
||||||
export namespace Functions {
|
export namespace Functions {
|
||||||
type ParametersImpl<fn> = fn extends (...args: infer args) => any
|
type ParametersImpl<fn> = fn extends (...args: infer args) => any
|
||||||
@@ -67,4 +67,62 @@ export namespace Functions {
|
|||||||
export interface ReturnTypeFn extends Fn {
|
export interface ReturnTypeFn extends Fn {
|
||||||
return: ReturnTypeImpl<this["arg0"]>;
|
return: ReturnTypeImpl<this["arg0"]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms the return type of a function type.
|
||||||
|
*
|
||||||
|
* @param fn - Type-level function to call on the return type.
|
||||||
|
* @param fnValue - The function type to update.
|
||||||
|
* @returns a function type with an updated return type.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* type T0 = Call<
|
||||||
|
* Functions.MapReturnType<Numbers.ToString>,
|
||||||
|
* (a: number, b: string) => 1 | 2
|
||||||
|
* >;
|
||||||
|
* // => (a: number, b: string) => "1" | "2"
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
export type MapReturnType<
|
||||||
|
fn extends Fn | unset | _ = unset,
|
||||||
|
fnValue extends ((...args: any[]) => any) | _ | unset = unset
|
||||||
|
> = PartialApply<MapReturnTypeFn, [fn, fnValue]>;
|
||||||
|
|
||||||
|
export interface MapReturnTypeFn extends Fn {
|
||||||
|
return: this["args"] extends [infer fn extends Fn, infer fnValue]
|
||||||
|
? fnValue extends (...args: infer args) => infer returnType
|
||||||
|
? (...args: args) => Call<fn, returnType>
|
||||||
|
: never
|
||||||
|
: never;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Transforms the paramaters of a function type.
|
||||||
|
*
|
||||||
|
* @param fn - Type-level function to call on parameters.
|
||||||
|
* @param fnValue - The function type to update.
|
||||||
|
* @returns a function type with updated parameters.
|
||||||
|
*
|
||||||
|
* @example
|
||||||
|
* ```ts
|
||||||
|
* type T0 = Call<
|
||||||
|
F.MapParameters<Tuples.Map<Strings.ToNumber>>,
|
||||||
|
(a: "1" | "2", b: "3" | "4") => void
|
||||||
|
>;
|
||||||
|
* // => (a: 1 | 2, b: 3 | 4) => void
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
export type MapParameters<
|
||||||
|
fn extends Fn | unset | _ = unset,
|
||||||
|
fnValue extends ((...args: any[]) => any) | _ | unset = unset
|
||||||
|
> = PartialApply<MapParametersFn, [fn, fnValue]>;
|
||||||
|
|
||||||
|
export interface MapParametersFn extends Fn {
|
||||||
|
return: this["args"] extends [infer fn extends Fn, infer fnValue]
|
||||||
|
? fnValue extends (...args: infer args) => infer returnType
|
||||||
|
? (...args: Extract<Call<fn, args>, readonly any[]>) => returnType
|
||||||
|
: never
|
||||||
|
: never;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { F, _ } from "../src/index";
|
import { F, Numbers, Strings, Tuples, _ } from "../src/index";
|
||||||
import { Call } from "../src/internals/core/Core";
|
import { Call } from "../src/internals/core/Core";
|
||||||
import { Equal, Expect } from "../src/internals/helpers";
|
import { Equal, Expect } from "../src/internals/helpers";
|
||||||
|
|
||||||
@@ -23,4 +23,20 @@ describe("Functions", () => {
|
|||||||
// ^?
|
// ^?
|
||||||
type tes1 = Expect<Equal<res1, boolean>>;
|
type tes1 = Expect<Equal<res1, boolean>>;
|
||||||
});
|
});
|
||||||
|
it("MapReturnType", () => {
|
||||||
|
type res1 = Call<
|
||||||
|
// ^?
|
||||||
|
F.MapReturnType<Strings.ToNumber>,
|
||||||
|
(a: string, b: number) => "1" | "2"
|
||||||
|
>;
|
||||||
|
type tes1 = Expect<Equal<res1, (a: string, b: number) => 1 | 2>>;
|
||||||
|
});
|
||||||
|
it("MapParameters", () => {
|
||||||
|
type res1 = Call<
|
||||||
|
// ^?
|
||||||
|
F.MapParameters<Tuples.Map<Strings.ToNumber>>,
|
||||||
|
(a: "1" | "2", b: "3" | "4") => void
|
||||||
|
>;
|
||||||
|
type tes1 = Expect<Equal<res1, (a: 1 | 2, b: 3 | 4) => void>>;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user