javascript - How to annotate a generic closure that returns an object with methods with Flow?
问题描述
This throws an error. It seems like the generic type T is not in scope in the push method?
function entries<T>() {
let entries: T[] = []
return {
push: (entry: T) => {
entries.push(entry)
}
}
}
let e = entries<string>()
e.push("abc") // <---- ERROR HERE
That throws the error:
14: e.push("abc")
^ Cannot call `e.push` with `"abc"` bound to `entry` because string [1] is incompatible with `T` [2]. [incompatible-call]
References:
14: e.push("abc")
^ [1]
6: push: (entry: T) => {
^ [2]
Here is the try flow link: https://flow.org/try/#0FAMwrgdgxgLglgewgAgKYRgJzqgzgHgBUA+ACgEpkBvYASABtUY0Ns8AuZQgbQF1kAvMj7A6mJmEwoatWgAcwuABadS6LAE9OhSgOLU6tZMfVtcAOgXK1rDeUMBfOk6fBGzVIJZYcBXD4gAczJ7YFRLRSVSACIAQwAjKGjyIA
解决方案
推荐阅读
- machine-learning - 问:Acumos:我不知道文档中写的“Acumos Portal Marketplace Users”和“Acumos Operations”有什么区别
- aem - 如何在aem中的两个组件之间设置组件?
- ios - 无论文本长度如何,如何使 SwiftUI 文本视图显示两行?
- ruby - 多个 Has_Many 与相同模型但不同列表的关系
- scala - 使用 sbt 运行 Scala/Swing 程序时出现 NoClassDefFoundError
- php - PHP警告:除以零
- android - 奖励产品是否将应用程序标记为“包含广告”?
- javascript - 使用 jQuery 检测 QR/条形码输入 onchange 事件
- mysql - 如何在 INSERT INTO table VALUES (....) 期间检查一个单词是否在一组允许的单词中?
- excel - 如何更改此基于 VBA/XSLT 的代码,以便将输出写入单个 XML 文件