typescript - typeof a == "array"有什么问题?

标签 typescript

尝试 playground :

typeof a == "array"

将导致

Operator '==' cannot be applied to types '"string" | "number" | "symbol" | "object" | "function"' and '"array"'.)

不仅这个错误消息对我来说毫无意义(typeof a == "object" 就好了),它似乎也是最近的更改。您现在必须使用 a instanceof Array,我上次检查时没有必要。你能给我解释一下吗?

最佳答案

正如 SLaks 所指出的,typeof 运算符永远不能返回“array”。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof

它从来没有用过,但 TypeScript 在 2.0 中变得足够聪明,可以警告你。

关于typescript - typeof a == "array"有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42569913/

相关文章:

unit-testing - Karma with Typescript - 无法获得代理来提供我的文件(或者我找不到它)

javascript - 在父级中使用 Object.assign 设置子属性

node.js - "SyntaxError: Cannot use import statement outside a module"依赖项

Angular Observable 等到它完成

javascript - Angular 6 : Data are not displayed in front end

typescript - Typescript 会支持带有 JavaScript 库的现有项目吗?

javascript - 覆盖 JavaScript 对象和函数的危险

angular2 react 形式 |未捕获的类型错误 : Cannot set property stack of [object Object] which has only a getter

angularjs - angular 2 中的验证,不从@angular/common 导入与表单验证相关的 ControlGroup 和 Control?

javascript - TypeScript:如何在构造函数中设置对象属性(取决于对象属性)