【发布时间】:2021-01-17 18:31:24
【问题描述】:
我希望创建一个函数 flowLens,它采用具有特殊签名/类型的函数数组。
- 函数只能有一个参数
- 函数的 arg 应该是一个普通对象
- 函数必须返回普通对象中所有相同的键/值类型
- 该函数可以返回额外的/新的属性。
这是我的尝试:
type Plain = {[key: string]: any};
type SpecialFunction = <T extends Plain, R extends T & Plain>(props: T) => R;
function flowLens (...fns: SpecialFunction[]) {}
const invalidType = (props: { a: number }) => true;
const invalidNotArgs = (props: { a: number }) => ({ ducks: 1 });
const validOne = (props: { a: number }) => ({...props});
const validTwo = (props: { a: number }) => ({ ...props, b: 1 });
flowLens(invalidType)
flowLens(invalidNotArgs)
flowLens(validOne)
flowLens(validTwo)
可以这样做吗?如果有怎么办?
【问题讨论】:
标签: typescript typescript-generics