Javascript导入句柄未定义

bmi*_*lac 5 javascript node.js es6-module-loader

是否有可能像下面的开关案例示例声明中那样捕获拼写错误?

首选方式是eslinter报告警告/错误.如果未定义,当前添加toString()到const可用于TypeError在运行时引发.

actionTypes.js

export const UPDATE_REQUEST = 'UPDATE_REQUEST';
Run Code Online (Sandbox Code Playgroud)

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;
  }
}
Run Code Online (Sandbox Code Playgroud)

更新:

正如@ n​​ikc.org回答eslint-plugin-import with namespace选项可以用于linting这样的bug.

这是一个包含配置和演示的小型存储库:

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

eslint配置的相关部分是:

"plugins": ["import"],
"rules": {
  "import/namespace": [2],
Run Code Online (Sandbox Code Playgroud)

nik*_*org 0

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

要完全消除这个问题,您可以看一下Tern,它可以在多个编辑器中为您提供代码洞察和代码补全,从而完全避免拼写错误。