是否可以在下面的 switch case 示例语句中捕获拼写错误?
首选方法是 eslinter 报告警告/错误。如果未定义,当前添加toString()
到const
可用于在运行时引发。TypeError
actionTypes.js
export const UPDATE_REQUEST = 'UPDATE_REQUEST';
减速器.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可用于检查此类错误。
这是带有配置和演示的小型存储库:
https://github.com/bmihelac/test-js-import-undefined/tree/eslint-plugin-import
eslint 配置的相关部分是:
"plugins": ["import"],
"rules": {
"import/namespace": [2],