我基本上是在尝试创建“检查器”功能,并根据该功能的结果调用另一个功能。此另一个函数只能接受单个类型。检查器功能应“过滤”不需要的类型。
这里是一个例子:
Private Sub TimeBox_AfterUpdate()
Dim m, lr As Long
Dim cell As Range
s = Me.TimeBox.Text
lr = Sheets("Sheet4").Range("A" & Rows.Count).End(xlUp).Row
For Each cell In Sheets("Sheet4").Range("A1:A" & lr)
If cell.Text = s Then
m = m + 1
End If
Next cell
If m = 0 Then
MsgBox "Time not found"
Else
MsgBox m
End If
End Sub
但是Flowtype不喜欢这样并返回此错误:
class A {}
class B {}
const checkB = (entity: A | B): boolean => {
return (entity instanceof B)
}
const execute = (b: B) => {
console.log('executed')
}
const start = (entity: A | B) => {
if (checkB(entity)) {
execute(entity)
}
}
const a = new A()
const b = new B()
start(a)
start(b)
我想知道是否有人可以说服流程以这种方式工作。链接到Flowtype REPL: https://flow.org/try/#0PTAEAEDMBsHsHcBQiDG0CGBnToCCoBvAX2TSxwCFCTVYA7TAF1BQAsBTFAayoF5QAFOzqMAlowCeALjygAPqAoBKGQCNYsaO3R1QvAHyFEoUACd2jAK6ndQkeImhRDRjpTtYkRUsQ0U9JlB2AA9OS0Z2PUFVGWU9QwJjFgDNdgA6OABzAQByELCIgBMcnz8A5iZ0U2Z+OzFJGXwFOIMjE1EvATZOHjqHJSU2kyDQlHD2PskfExIyl1B0KLp2eDwBH3951SWVxXXkSuqBdB9DxgFVHyA
编辑:我的环境是使用Flow v。execute(entity)
^ Cannot call `execute` with `entity` bound to `b` because `A` [1] is incompatible with `B` [2].
References:
15: const start = (entity: A | B) => {
^ [1]
11: const execute = (b: B) => {
^ [2]
设置的,但是正如您在REPL中所看到的那样,它也不适用于0.72.0
。
2018年11月26日更新:
似乎有未记录的0.86.0
注释可以这样工作:https://github.com/facebook/flow/issues/4723#issuecomment-325157852
%checks
这不会引发更多错误。但是,这有一些问题:
// @flow
class A {}
class B {}
const checkB = (entity: A | B): boolean %checks => {
return (entity instanceof B)
}
const execute = (b: B) => {
console.log('executed')
}
const start = (entity: A | B) => {
if (checkB(entity)) {
execute(entity)
}
}
const a = new A()
const b = new B()
start(a)
start(b)
函数位于不同的模块/文件中,则答案 0 :(得分:0)
看起来像this opened issue(也考虑添加您的案子:))。
但是,如果您删除方法checkB
并精确地在start
中进行细化,则会works well:
const start = (entity: A | B) => {
if (entity instanceof B) {
execute(entity)
}
}
如果您有更复杂的类型细化并且需要将其提取到单独的方法中,则可以将checkB
更改为getB
,就像null refinements seems to works well:
const getB = (entity: A | B): ?B => {
return (entity instanceof B) ? entity : null;
}
const execute = (b: B) => {
console.log('executed')
}
const start = (entity: A | B) => {
const b = getB(entity);
if (b) {
execute(b)
}
}