typescript - 管道函数参数类型被推断为任何
问题描述
基于我测试的高阶函数类型推断中的示例,TS 如何解析函数中的函数组合类型pipe
。pipe
:
declare function pipe<A extends any[], B, C>(
ab: (...args: A) => B, bc: (b: B) => C): (...args: A) => C;
这些是传递给管道的函数:
declare function list<T>(a: T): T[];
declare function box<V>(x: V): { value: V };
declare function numberBox(x: number[]): { value: number[] }
box
具有泛型函数的 PR 示例正确地强制执行参数类型listBox(1)
为number
:
const listBox = pipe(list, box); // T is retained
const x1 = listBox(1); // ... argument properly inferred to number here
但是当我使用非泛型number[]
框函数时,参数获取any
类型:
const listNumberBox = pipe(list, numberBox)
const x21 = listNumberBox(1) // argument has any type
const x22 = listNumberBox("asdfasd") // this shouldn't be possible :/
我会假设具体类型的推断实际上比泛型类型(?)“更容易”,所以
1.) 为什么最后一个示例的参数类型推断为any
?
2.) 如何更改函数签名pipe
以正确执行所有类型?
解决方案
推荐阅读
- angular - Angular6 ngModelChange 中的去抖动时间
- java - 如何使用 Java 从 Linux 中的 docx 中获取文本数据和表格数据?
- php - 如何“正则表达式”选择菜单中的所有选项
- javascript - 当数据未定义或为空时如何使用 Lodash
- node.js - 是否有任何 api (nodejs/python) 允许与给定电子邮件共享 google sheet(给定工作表 id)
- nginx - NGINX 上的每个 IP 地址每秒计算请求数
- batch-file - 批处理脚本中的 {{var}} 是什么意思
- swagger - 如何在 Swagger open api 3.0 中定义常量字符串
- react-native - React Native get undefined is not an object 错误
- c# - 在 Unity 2017.3.1f 中找不到排行榜