我从Flow得到一个我不明白的错误。
Cannot call document.createTreeWalker because:
• Either number literal 4 [1] is incompatible with number literal 1 [2].
• Or number literal 4 [1] is incompatible with number literal 5 [3].
• Or number literal 4 [1] is incompatible with number literal 128 [4].
• Or number literal 4 [1] is incompatible with number literal 129 [5].
• Or number literal 4 [1] is incompatible with number literal 132 [6].
• Or number literal 4 [1] is incompatible with number literal 133 [7].
hello.js
2│
3│ const body = document.body;
4│ if (body) {
5│ document.createTreeWalker(body, NodeFilter.SHOW_TEXT, null, false);
6│ }
7│
/tmp/flow/flowlib_18704f10/dom.js
[2] 1050│ createTreeWalker<RootNodeT: Node>(root: RootNodeT, whatToShow: 1, filter?: NodeFilterInterface, entityReferenceExpansion?: boolean): TreeWalker<RootNodeT, Element>;
1051│ createTreeWalker<RootNodeT: Node>(root: RootNodeT, whatToShow: 4, filter?: NodeFilterInterface, entityReferenceExpansion?: boolean): TreeWalker<RootNodeT, Text>;
[3] 1052│ createTreeWalker<RootNodeT: Node>(root: RootNodeT, whatToShow: 5, filter?: NodeFilterInterface, entityReferenceExpansion?: boolean): TreeWalker<RootNodeT, Element|Text>;
[4] 1053│ createTreeWalker<RootNodeT: Node>(root: RootNodeT, whatToShow: 128, filter?: NodeFilterInterface, entityReferenceExpansion?: boolean): TreeWalker<RootNodeT, Comment>;
[5] 1054│ createTreeWalker<RootNodeT: Node>(root: RootNodeT, whatToShow: 129, filter?: NodeFilterInterface, entityReferenceExpansion?: boolean): TreeWalker<RootNodeT, Element|Comment>;
[6] 1055│ createTreeWalker<RootNodeT: Node>(root: RootNodeT, whatToShow: 132, filter?: NodeFilterInterface, entityReferenceExpansion?: boolean): TreeWalker<RootNodeT, Text|Comment>;
[7] 1056│ createTreeWalker<RootNodeT: Node>(root: RootNodeT, whatToShow: 133, filter?: NodeFilterInterface, entityReferenceExpansion?: boolean): TreeWalker<RootNodeT, Text|Element|Comment>;
:
[1] 3508│ static SHOW_TEXT: 4;
在我看来,应该允许SHOW_TEXT
(即4),因为第1051行将4列为第二个参数的有效值。我错过了什么?
要重现这一点,您可以运行:
git clone https://github.com/MatrixFrog/flow-example
cd flow-example
git checkout 0f6035a8bb05cc8f0f01418d4617973991baa231
flow version # 0.73.0
flow check
答案 0 :(得分:1)
错误是如此无益的事实似乎是Flow的一个问题,但你的错误是那个
filter?: NodeFilterInterface
接受过滤器,或undefined
,null
不是有效值,所以
document.createTreeWalker(body, NodeFilter.SHOW_TEXT, undefined, false);
会正确通过typechecker。