【发布时间】:2021-04-25 10:53:45
【问题描述】:
function Box(x: any) {
return {
inspect: () => `Box(${x})`,
map: (f: (arg0: any) => any) => Box(f(x)),
fold: (f: (arg0: any) => any) => f(x)
}
}
const expr = Box(8)
.map((x: number) => x + 8)
.map((x: any) => Number(x))
.map((x) => Math.cos(x))
console.log(expr.fold((x: any) => x))
如何改进 Box 表达式的类型化?
【问题讨论】:
-
看看generics
-
你已经标记了这个 [typescript-generics],所以你似乎已经知道该怎么做了。您能否向我们展示您的尝试,或者解释一下您对使用泛型的不理解?
-
请注意 I cannot reproduce 你的 ESLint 警告;也许您的 IDE 配置有些奇怪?也许this 是你的问题?
-
每个帖子只问一个问题。我已经删除了关于 ESLint 的内容,您可能需要create a new question 来获取它(但请提供有关警告发生方式和位置的更多详细信息)
标签: typescript functional-programming typescript-typings typescript-generics