functions: add MapReturnType and MapParameters

This commit is contained in:
gvergnaud
2023-07-03 11:56:06 +02:00
parent dbfa37e05a
commit c35d8ab877
3 changed files with 78 additions and 2 deletions

View File

@@ -138,6 +138,8 @@ type res5 = Pipe<
- [x] `ReturnType<Fn>`
- [x] `Parameters<Fn>`
- [x] `Parameter<N, Fn>`
- [x] `MapReturnType<Fn, FunctionType>`
- [x] `MapParameters<Fn, FunctionType>`
- [ ] Tuples
- [x] `Create<X> -> [X]`
- [x] `Partition<Tuple>`

View File

@@ -1,4 +1,4 @@
import { Fn, PartialApply, unset, _ } from "../core/Core";
import { Fn, PartialApply, unset, _, Call } from "../core/Core";
export namespace Functions {
type ParametersImpl<fn> = fn extends (...args: infer args) => any
@@ -67,4 +67,62 @@ export namespace Functions {
export interface ReturnTypeFn extends Fn {
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;
}
}

View File

@@ -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 { Equal, Expect } from "../src/internals/helpers";
@@ -23,4 +23,20 @@ describe("Functions", () => {
// ^?
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>>;
});
});