babel报错Invalid typeof value: array, 是bug还是特性?

原因是写了typeof xxx == 'array', 虽然不能这么用,但是也不应报错啊,这是babel的bug还是特性?

Invalid typeof value: array
    at createTypeAnnotationBasedOnTypeof (C:\Front\fe\node_modules\@babel\types\lib\builders\flow\createTypeAnnotationBasedOnTypeof.js:29:9)
    at inferAnnotationFromBinaryExpression (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\inferer-reference.js:107:10)
    at getConditionalAnnotation (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\inferer-reference.js:138:20)
    at getTypeAnnotationBindingConstantViolations (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\inferer-reference.js:35:20)
    at NodePath._default (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\inferer-reference.js:22:14)
    at NodePath._getTypeAnnotation (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\index.js:77:22)
    at NodePath.getTypeAnnotation (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\index.js:41:15)
    at NodePath.ConditionalExpression (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\inferers.js:117:49)
    at NodePath._getTypeAnnotation (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\index.js:77:22)
    at NodePath.getTypeAnnotation (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\index.js:41:15)
    at NodePath.VariableDeclarator (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\inferers.js:62:27)
    at NodePath._getTypeAnnotation (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\index.js:77:22)
    at NodePath.getTypeAnnotation (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\index.js:41:15)
    at getTypeAnnotationBindingConstantViolations (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\inferer-reference.js:44:28)
    at NodePath._default (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\inferer-reference.js:22:14)
    at NodePath._getTypeAnnotation (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\index.js:77:22)
    at NodePath.getTypeAnnotation (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\index.js:41:15)
    at NodePath.isGenericType (C:\Front\fe\node_modules\@babel\traverse\lib\path\inference\index.js:136:21)
    at PluginPass.ForOfStatement (C:\Front\fe\node_modules\@babel\plugin-transform-for-of\lib\index.js:143:48)
    at newFn (C:\Front\fe\node_modules\@babel\traverse\lib\visitors.js:159:14)
    at NodePath._call (C:\Front\fe\node_modules\@babel\traverse\lib\path\context.js:46:20)
    at NodePath.call (C:\Front\fe\node_modules\@babel\traverse\lib\path\context.js:36:17)
    at NodePath.visit (C:\Front\fe\node_modules\@babel\traverse\lib\path\context.js:82:31)
    at TraversalContext.visitQueue (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:86:16)
    at TraversalContext.visitMultiple (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:61:17)
    at TraversalContext.visit (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:107:19)
    at traverseNode (C:\Front\fe\node_modules\@babel\traverse\lib\traverse-node.js:22:17)
    at NodePath.visit (C:\Front\fe\node_modules\@babel\traverse\lib\path\context.js:88:52)
    at TraversalContext.visitQueue (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:86:16)
    at TraversalContext.visitSingle (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:65:19)
    at TraversalContext.visit (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:109:19)
    at traverseNode (C:\Front\fe\node_modules\@babel\traverse\lib\traverse-node.js:22:17)
    at NodePath.visit (C:\Front\fe\node_modules\@babel\traverse\lib\path\context.js:88:52)
    at TraversalContext.visitQueue (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:86:16)
    at TraversalContext.visitSingle (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:65:19)
    at TraversalContext.visit (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:109:19)
    at traverseNode (C:\Front\fe\node_modules\@babel\traverse\lib\traverse-node.js:22:17)
    at NodePath.visit (C:\Front\fe\node_modules\@babel\traverse\lib\path\context.js:88:52)
    at TraversalContext.visitQueue (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:86:16)
    at TraversalContext.visitMultiple (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:61:17)
    at TraversalContext.visit (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:109:19)
    at traverseNode (C:\Front\fe\node_modules\@babel\traverse\lib\traverse-node.js:22:17)
    at NodePath.visit (C:\Front\fe\node_modules\@babel\traverse\lib\path\context.js:88:52)
    at TraversalContext.visitQueue (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:86:16)
    at TraversalContext.visitMultiple (C:\Front\fe\node_modules\@babel\traverse\lib\context.js:61:17)
阅读 820
1 个回答

用了 Flow 吗?

确实会抛异常:https://github.com/babel/babel/blob/main/packages/babel-types/src/builders/flow/createTypeAnnotationBasedOnTypeof.ts#L47

目前看来是故意设计如此,但没研究为啥要这么设计。


【补充】

又仔细看了一下,虽然该源码所在的文件被放到了 Flow 目录下,但实际从主入口就会被调用,也就是说 TypeScript 同样会走这块的逻辑。

另外翻到了一个类似的 Issue:https://github.com/babel/babel/issues/5178 跟题目中的场景差不多,只是他是这么写的:

if (typeof a === '') {
  // balabala
}

最下面有一位回复者 kangax 是 babel 的核心贡献者之一,宣称会修复这个 bug,但这个 2017 年提的 Issue,至今仍未被 Close…… 😂

推荐问题
logo
Microsoft
子站问答
访问
宣传栏