简体   繁体   English

具有返回类型应用于下一个函数的函数数组

[英]Array of functions with return types applied to next function

I am looking to create a unique type of array that contains functions. 我正在寻找创建包含函数的唯一类型的数组。

const a = [
  (): { a: string } => ({ a: 'alpha'}),
  ({ a }): { b: string } => ({ b: 'beta' }),
  ({ a, b }): {} => ({}),
]

The functions have explicit return types, what I'd like is a way to not have to specify input types, and have the input types "chain" in a way so that all of the input types are a Merge of all the return types for the previous functions in the stack. 这些函数具有显式的返回类型,我想要的是一种不必指定输入类型的方法,并以某种方式将输入类型“链化”,以使所有输入类型都是针对所有返回类型的Merge堆栈中的先前功能。

If this isn't possible, at least would it be possible to have a interface like this: 如果这不可能,那么至少可以有这样的接口:

interface State {
  a: string,
  b: string
}

And create a generic type for the array that takes State and applies it as a partial to all input and return types for each function in the array? 并为采用State的数组创建通用类型,并将其作为部分应用于数组中每个函数的所有输入和返回类型?

You can create a chain of function with one function in the chain depending on a previous function but you need a function and overloads for each number functions you want to support: 您可以根据先前的功能创建一个功能链,其中的一个功能取决于上一个功能,但是您需要一个功能以及要支持的每个数字功能的重载:

function chain<R1, R2, R3, R4>(fn1: ()=> R1, fn2: (a: R1) => R2, fn3: (a: R1 & R2) => R3, fn4: (a: R1 & R2 & R3) => R4) : [typeof fn1, typeof fn2, typeof fn3, typeof fn4]
function chain<R1, R2, R3>(fn1: ()=> R1, fn2: (a: R1) => R2, fn3: (a: R1 & R2) => R3) : [typeof fn1, typeof fn2, typeof fn3]
function chain<R1, R2>(fn1: ()=> R1, fn2: (a: R1) => R2) : [typeof fn1, typeof fn2]
function chain(...fns: Array<(a?: any)=> any>) : Array<(a?: any)=> any> {
    return fns;
}

const a =chain( // return types are not necessary.
    () => ({ a: 'alpha'}),
    ({ a })  => ({ b: 'beta' }),
    ({ a, b }) => ({ c: a, z:a }),
)

Not great but this offers some support: 不太好,但这提供了一些支持:

type JourneyFn<T> = (s: Partial<T>) => Partial<T> | Promise<Partial<T>>;
type JourneyFns<T> = JourneyFn<T>[]

interface State {
  a: string,
  b: string
}

const a: JourneyFns<State> = [
  () => ({ a: 'alpha'}),
  ({ a }) => ({ b: 'beta' }),
  ({ a, b }): {} => ({}),
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据 function 的返回值推断类型,其中 function 参数是函数的记录 - Infer types on return value of function, where function parameter is a record of functions 返回参数类型为传入 function 参数值的函数的 object - Return object of functions with argument types of incoming function argument values Typescript:键入一组部分应用函数,其参数数量和类型可变,均返回相同类型的 function - Typescript: Type a group of partially applied functions with variable number and type of parameters that all return function of same type 使用返回多种类型的函数 - Working with functions that return multiple types 工厂函数的命名返回类型? - Named return types for factory functions? Typescript Array.isArray 保护语句和 function 上的返回类型 - Typescript Array.isArray guard statement and return types on function Function 返回从数组参数中输入的键定义的选择类型 - Function to return picked types defined from the keys entered in array argument 将类型数组转换为 typescript 中的函数参数类型 - Transform array of types into functions parameters types in typescript 使用映射类型返回函数返回类型的对象 - Using mapped types to return object of return types of functions 运算符 '&gt;' 不能应用于类型 'boolean' 和 '{ return: Observable<any> ; }' 错误</any> - Operator '>' cannot be applied to types 'boolean' and '{ return: Observable<any>; }' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM