Javascript导入句柄未定义

是否有可能在下面的switch case例子语句中发现错别字?

首选的方法是,eslinter报告警告/错误。 如果未定义,则可以使用toString() to const来在运行时TypeError

actionTypes.js

 export const UPDATE_REQUEST = 'UPDATE_REQUEST'; 

reducer.js

 import * as types from '../constants/actionTypes'; export default function pouchdbReducer(state = {}, action) { switch (action.type) { case types.UPDDATE_REQUEST: // there is a typo above and it evaluates to `undefined` // this code would never be reached - how to make it an error return Object.assign({}, state, {updated: true}); default: return state; } } 

更新:

作为@ nikc.org回答eslint-plugin-import 命名空间选项可用于linting这样的错误。

这里是configuration和演示的小型仓库:

https://github.com/bmihelac/test-js-import-undefined/tree/eslint-plugin-import

eslint config的相关部分是:

 "plugins": ["import"], "rules": { "import/namespace": [2], 

免责声明,我是tern-lint的授权者。

我build议你使用tern-lint ,可以报告“未知财产”等错误。

你可以使用这个linter命令或者一个支持它的编辑器(Emacs,Atom,CodeMirror或者Eclipse)。 这里有一个Eclipse tern.java的截图

在这里输入图像描述

ESLint的eslint-plugin-import插件对importexport进行静态分析,并且可以configuration为根据不同的条件引发错误或警告,包括您在问题中拼写引用导入符号的情况。

为了完全消除这个问题,你可以看一下Tern ,它可以让你在几个编辑器中完成代码识别和代码完成,从而避免拼写错误。

我想根据动作types的数量(如果你愿意把它们分开),你可以稍微改变你如何导入它们。

也许是这样的:

 import {TYPE_ONE, TYPE_TWO} from '../constants/firstActionTypes'; import {TYPE_THREE} from '../constants/secondActionTypes'; export default function pouchdbReducer(state = {}, action) { switch (action.type) { case TTYPE_ONE: // Linting should identify this easily now return Object.assign({}, state, {updated: true}); default: return state; } } 

典型的绒毛可以很容易地拿起来,一个好处是,你可以分解你的行动,在他们的顾虑。