如何在 Typescript 中使用 compose?
How do I use compose in Typescript?
我在 Typescript 中使用 compose 时遇到问题....
const rawHeaders = R.compose(
R.join('\n'),
R.map(x=>`${x[0]}: ${x[1]}`),
R.toPairs
)
我已经尝试了下面的方法,但它很残酷。有谁知道让这项工作更优雅的方法吗?
const rawHeaders:Function = R.compose(
R.join('\n'),
R.map((x:[String, String]) =>`${x[0]}: ${x[1]}`),
(x:{s:String})=>R.toPairs(x))
)
我也试过用ts-ignore,目前看来是最好的选择。
const rawHeaders = R.compose(
R.join('\n'),
// @ts-ignore
R.map(x=>`${x[0]}: ${x[1]}`),
// @ts-ignore
R.toPairs
)
您是否尝试过利用 compose 本身的类型?您可以像这样给它提供参数和每个函数的 return 值:
const rawHeaders = R.compose<
{ [key: string]: string | number }, // Argument
Array<[string, string]>, // Return of toPairs
string[], // Return of map
string // Return of join
>(
R.join('\n'),
R.map(x => `${x[0]}: ${x[1]}`),
R.toPairs
);
就我个人而言,我更喜欢使用 pipe
,因为打字与 pipe
中的参数顺序相匹配,而 compose
则相反:
const rawHeaders = R.pipe<
{ [key: string]: string | number }, //Argument
Array<[string, string]>, // return of toPairs
string[], // return of map
string // return of join
>(
R.toPairs,
R.map(x => `${x[0]}: ${x[1]}`),
R.join('\n')
);
无论哪种方式,pipe/compose 中的每个函数都会获得正确的值,并且您不需要专门装饰管道中的函数(除非您开始使用 R.flip
之类的东西)。它很冗长,但它有效。
(您可以为第一个函数指定任意数量的参数,顺便说一句,重载将处理其余部分)
我在 Typescript 中使用 compose 时遇到问题....
const rawHeaders = R.compose(
R.join('\n'),
R.map(x=>`${x[0]}: ${x[1]}`),
R.toPairs
)
我已经尝试了下面的方法,但它很残酷。有谁知道让这项工作更优雅的方法吗?
const rawHeaders:Function = R.compose(
R.join('\n'),
R.map((x:[String, String]) =>`${x[0]}: ${x[1]}`),
(x:{s:String})=>R.toPairs(x))
)
我也试过用ts-ignore,目前看来是最好的选择。
const rawHeaders = R.compose(
R.join('\n'),
// @ts-ignore
R.map(x=>`${x[0]}: ${x[1]}`),
// @ts-ignore
R.toPairs
)
您是否尝试过利用 compose 本身的类型?您可以像这样给它提供参数和每个函数的 return 值:
const rawHeaders = R.compose<
{ [key: string]: string | number }, // Argument
Array<[string, string]>, // Return of toPairs
string[], // Return of map
string // Return of join
>(
R.join('\n'),
R.map(x => `${x[0]}: ${x[1]}`),
R.toPairs
);
就我个人而言,我更喜欢使用 pipe
,因为打字与 pipe
中的参数顺序相匹配,而 compose
则相反:
const rawHeaders = R.pipe<
{ [key: string]: string | number }, //Argument
Array<[string, string]>, // return of toPairs
string[], // return of map
string // return of join
>(
R.toPairs,
R.map(x => `${x[0]}: ${x[1]}`),
R.join('\n')
);
无论哪种方式,pipe/compose 中的每个函数都会获得正确的值,并且您不需要专门装饰管道中的函数(除非您开始使用 R.flip
之类的东西)。它很冗长,但它有效。
(您可以为第一个函数指定任意数量的参数,顺便说一句,重载将处理其余部分)