禁止将字面量作为异常抛出。
规则配置
// code-linter.json5
{
"rules": {
"@typescript-eslint/no-throw-literal": "error"
}
} 正例
// 抛出Error对象
throw new Error();
const e = new Error('error');
throw e;
const err1 = new Error();
throw err1;
function err2() {
return new Error();
}
throw err2();
class CustomError extends Error {
// ...
}
throw new CustomError(); 反例
throw 'error';
throw 0;
throw undefined;
throw null;
const err1 = new Error();
throw 'an ' + err1;
const err2 = new Error();
throw `${err2}`;
const err3 = '';
throw err3;
function err() {
return '';
}
throw err();