假设我有一个函数,它使用两个函数f
和g
作为参数,并返回一个执行f
和g
并返回结果的对象的函数。我还想强制f
和g
具有相同的签名。使用条件类型,这很容易:
type ArgumentTypes<F extends Function> = F extends (...args: infer A) => any ? A : never;
function functionPair<
F extends (...args: any[]) => any,
G extends (...args: ArgumentTypes<F>) => any
>
(f: F, g: G): (...args: ArgumentTypes<F>) => { f: ReturnType<F>, g: ReturnType<G> }
{
return (...args: ArgumentTypes<F>) => ({ f: f(...args), g: g(...args) });
}
functionPair((foo: string) => foo, (bar: number) => bar); // Error, incompatible signatures, as expected
functionPair((foo: string) => foo, (bar: string) => bar.length); // (foo: string) => { f: string; g: number; }, as expected
现在,如果我想使f
和g
为可选 ,并且要使返回的对象的形状发生变化,该怎么办?也就是说,如果f
或g
为undefined
,则应从结果对象中缺少其键:
functionPair(); // Should be () => {}
functionPair(undefined, undefined); // Should be () => {}
functionPair((foo: string) => foo); // Should be (foo: string) => { f: string }
functionPair(undefined, (bar: string) => foo.length); // Should be (bar: string) => { g: number }
functionPair((foo: string) => foo, (bar: string) => foo.length); // Should be (foo: string) => { f: string, g: number }, as before
我一直在尝试使用条件类型来完成此操作,但是在条件上强制执行结果函数的形状时遇到了一些麻烦。到目前为止,这是我所拥有的(严格禁用null检查):
function functionPair<
A extends F extends undefined ? G extends undefined ? [] : ArgumentTypes<G> : ArgumentTypes<F>,
F extends (...args: any[]) => any = undefined,
G extends F extends undefined ? (...args: any[]) => any : (...args: ArgumentTypes<F>) => any = undefined
>
(f?: F, g?: G): (...args: A) =>
F extends undefined
? G extends undefined ? {} : { g: ReturnType<G> }
: G extends undefined ? { f: ReturnType<F> } : { f: ReturnType<F>, g: ReturnType<G> }
{ /* implementation... */ }
const a = functionPair(); // () => {}, as expected
const b = functionPair((foo: string) => foo); // (foo: string) => { f: string; }, as expected
const c = functionPair((foo: string) => foo, (bar: number) => bar); // Error, incompatible signatures, as expected
const d = functionPair((foo: string) => foo, (bar: string) => bar.length); // (foo: string) => { f: string; g: number; }, as expected
const e = functionPair(undefined, undefined); // INCORRECT! Expected () => {}, got (...args: unknown[] | []) => {} | { f: any; } | { g: any; } | { f: any; g: any; }
const f = functionPair(undefined, (bar: string) => bar.length); // INCORRECT! Expected (bar: string) => { g: number; } but got (...args: unknown[] | [string]) => { g: number; } | { f: any; g: number; }
顺便说一句,我知道从技术上讲,重载是可行的,如下所示,但我真的很想了解没有重载的情况。
function functionPairOverloaded(): () => {}
function functionPairOverloaded(f: undefined, g: undefined): () => {}
function functionPairOverloaded<F extends (...args: any[]) => any>(f: F): (...args: ArgumentTypes<F>) => { f: ReturnType<F> }
function functionPairOverloaded<G extends (...args: any[]) => any>(f: undefined, g: G): (...args: ArgumentTypes<G>) => { g: ReturnType<G> }
function functionPairOverloaded<F extends (...args: any[]) => any, G extends (...args: ArgumentTypes<F>) => any>(f: F, g: G): (...args: ArgumentTypes<F>) => { f: ReturnType<F>, g: ReturnType<G> }
function functionPairOverloaded<F extends (...args: any[]) => any, G extends (...args: any[]) => any>(f?: F, g?: G) { /* implementation... */ }
答案 0 :(得分:1)
假设您已打开--strictNullChecks
,我想我会这样做:
type Fun = (...args: any[]) => any;
type FunFrom<F, G> = F extends Fun ? F : G extends Fun ? G : () => {};
type IfFun<F, T> = F extends Fun ? T : never;
type Ret<T> = T extends (...args: any[]) => infer R ? R : never
declare function functionPair<
F extends Fun | undefined = undefined,
G extends ((...args: (F extends Fun ? Parameters<F> : any[])) => any)
| undefined = undefined
>(
f?: F,
g?: G
): (...args: Parameters<FunFrom<F, G>>) => {
[K in IfFun<F, 'f'> | IfFun<G, 'g'>]: K extends 'f' ? Ret<F> : Ret<G>
};
这很丑陋,但确实可以为您提供您想要的行为:
const a = functionPair(); // () => {}, as expected
const b = functionPair((foo: string) => foo); // (foo: string) => { f: string; }, as expected
const c = functionPair((foo: string) => foo, (bar: number) => bar); // Error, incompatible signatures, as expected
const d = functionPair((foo: string) => foo, (bar: string) => bar.length); // (foo: string) => { f: string; g: number; }, as expected
const e = functionPair(undefined, undefined); // () => {}, as expected
const f = functionPair(undefined, (bar: string) => bar.length); // (bar: string) => { g: number; }, as expected
我决定只使用两个类型参数F
和G
,而不是A
使用Parameters<FunFrom<F, G>>
。请注意,Parameters
是类似于ArgumentTypes
的内置类型函数。
另外,对于返回函数的返回类型,我做了一个难看的映射类型。我首先打算做类似IfFun<F, {f: Ret<F>}> & IfFun<G, {g: Ret<G>}>
的事情,(我相信)它更容易理解,但是生成的类型{f: X, g: Y}
比交点{f: X} & {g: Y}
更好。
无论如何,希望能有所帮助。祝你好运!
如果您希望能够关闭--strictNullChecks
,则定义会变得更加毛茸茸:
type Fun = (...args: any[]) => any;
type AsFun<F> = [F] extends [Fun] ? F : never
type FunFrom<F, G> = AsFun<IfFun<F, F, IfFun<G, G, () => {}>>>;
type IfFun<F, Y, N=never> = F extends undefined ? N :
0 extends (1 & F) ? N : F extends Fun ? Y : N;
type Ret<T> = T extends (...args: any[]) => infer R ? R : never
declare function functionPair<
F extends Fun | undefined = undefined,
G extends ((...args: IfFun<F, Parameters<F>, any[]>) => any)
| undefined = undefined
>(
f?: F,
g?: G
): (...args: Parameters<FunFrom<F, G>>) => {
[K in IfFun<F, 'f'> | IfFun<G, 'g'>]: K extends 'f' ? Ret<F> : Ret<G>
};
区别在于IfFun<>
必须能够将功能与undefined
和any
区分开,当您关闭--strictNullChecks
时,这两个功能会在不幸的地方弹出。这是因为undefined extends Function ? true : false
开始返回true
,并且当您将手动any
值传递给函数时undefined
开始被推断。区分undefined
相当简单,因为Function extends undefined ? true : false
仍然是false
,但是区分any
却很烦人并且涉及一些funny business。
祝你好运!