diff --git a/packages/eslint-plugin/docs/rules/no-throw-literal.mdx b/packages/eslint-plugin/docs/rules/no-throw-literal.mdx index b5d57c5533f..d5919b3bdc6 100644 --- a/packages/eslint-plugin/docs/rules/no-throw-literal.mdx +++ b/packages/eslint-plugin/docs/rules/no-throw-literal.mdx @@ -12,104 +12,14 @@ import TabItem from '@theme/TabItem'; It is considered good practice to only `throw` the `Error` object itself or an object using the `Error` object as base objects for user-defined exceptions. The fundamental benefit of `Error` objects is that they automatically keep track of where they were built and originated. -This rule restricts what can be thrown as an exception. When it was first created, it only prevented literals from being thrown (hence the name), but it has now been expanded to only allow expressions which have a possibility of being an `Error` object. With the `allowThrowingAny` and `allowThrowingUnknown`, it can be configured to only allow throwing values which are guaranteed to be an instance of `Error`. +This rule restricts what can be thrown as an exception. -## Examples +:::warning +This rule is being renamed to [`only-throw-error`](./only-throw-error.mdx). +When it was first created, it only prevented literals from being thrown (hence the name), but it has now been expanded to only allow expressions which have a possibility of being an `Error` object. +With the `allowThrowingAny` and `allowThrowingUnknown` options, it can be configured to only allow throwing values which are guaranteed to be an instance of `Error`. -This rule is aimed at maintaining consistency when throwing exception by disallowing to throw literals and other expressions which cannot possibly be an `Error` object. - - - - -```ts -throw 'error'; - -throw 0; - -throw undefined; - -throw null; - -const err = new Error(); -throw 'an ' + err; - -const err = new Error(); -throw `${err}`; - -const err = ''; -throw err; - -function err() { - return ''; -} -throw err(); - -const foo = { - bar: '', -}; -throw foo.bar; -``` - - - - -```ts -throw new Error(); - -throw new Error('error'); - -const e = new Error('error'); -throw e; - -try { - throw new Error('error'); -} catch (e) { - throw e; -} - -const err = new Error(); -throw err; - -function err() { - return new Error(); -} -throw err(); - -const foo = { - bar: new Error(), -}; -throw foo.bar; - -class CustomError extends Error { - // ... -} -throw new CustomError(); -``` - - - - -## Options - -This rule adds the following options: - -```ts -interface Options { - /** - * Whether to always allow throwing values typed as `any`. - */ - allowThrowingAny?: boolean; - - /** - * Whether to always allow throwing values typed as `unknown`. - */ - allowThrowingUnknown?: boolean; -} - -const defaultOptions: Options = { - allowThrowingAny: false, - allowThrowingUnknown: false, -}; -``` +The current name `no-throw-literal` will be removed in a future major version of typescript-eslint. +::: {/* Intentionally Omitted: When Not To Use It */} diff --git a/packages/eslint-plugin/docs/rules/only-throw-error.mdx b/packages/eslint-plugin/docs/rules/only-throw-error.mdx new file mode 100644 index 00000000000..fd9d8f2c4d2 --- /dev/null +++ b/packages/eslint-plugin/docs/rules/only-throw-error.mdx @@ -0,0 +1,115 @@ +--- +description: 'Disallow throwing non-`Error` values as exceptions.' +--- + +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; + +> 🛑 This file is source code, not the primary documentation location! 🛑 +> +> See **https://typescript-eslint.io/rules/only-throw-error** for documentation. + +It is considered good practice to only `throw` the `Error` object itself or an object using the `Error` object as base objects for user-defined exceptions. +The fundamental benefit of `Error` objects is that they automatically keep track of where they were built and originated. + +This rule restricts what can be thrown as an exception. + +## Examples + +This rule is aimed at maintaining consistency when throwing exception by disallowing to throw literals and other expressions which cannot possibly be an `Error` object. + + + + +```ts +throw 'error'; + +throw 0; + +throw undefined; + +throw null; + +const err = new Error(); +throw 'an ' + err; + +const err = new Error(); +throw `${err}`; + +const err = ''; +throw err; + +function err() { + return ''; +} +throw err(); + +const foo = { + bar: '', +}; +throw foo.bar; +``` + + + + +```ts +throw new Error(); + +throw new Error('error'); + +const e = new Error('error'); +throw e; + +try { + throw new Error('error'); +} catch (e) { + throw e; +} + +const err = new Error(); +throw err; + +function err() { + return new Error(); +} +throw err(); + +const foo = { + bar: new Error(), +}; +throw foo.bar; + +class CustomError extends Error { + // ... +} +throw new CustomError(); +``` + + + + +## Options + +This rule adds the following options: + +```ts +interface Options { + /** + * Whether to always allow throwing values typed as `any`. + */ + allowThrowingAny?: boolean; + + /** + * Whether to always allow throwing values typed as `unknown`. + */ + allowThrowingUnknown?: boolean; +} + +const defaultOptions: Options = { + allowThrowingAny: false, + allowThrowingUnknown: false, +}; +``` + +{/* Intentionally Omitted: When Not To Use It */} diff --git a/packages/eslint-plugin/src/configs/all.ts b/packages/eslint-plugin/src/configs/all.ts index b8711f0cc6a..1e7304e45a3 100644 --- a/packages/eslint-plugin/src/configs/all.ts +++ b/packages/eslint-plugin/src/configs/all.ts @@ -90,8 +90,6 @@ export = { 'no-shadow': 'off', '@typescript-eslint/no-shadow': 'error', '@typescript-eslint/no-this-alias': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error', '@typescript-eslint/no-unnecessary-condition': 'error', '@typescript-eslint/no-unnecessary-qualifier': 'error', @@ -118,6 +116,8 @@ export = { '@typescript-eslint/no-useless-template-literals': 'error', '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/non-nullable-type-assertion-style': 'error', + 'no-throw-literal': 'off', + '@typescript-eslint/only-throw-error': 'error', '@typescript-eslint/parameter-properties': 'error', '@typescript-eslint/prefer-as-const': 'error', 'prefer-destructuring': 'off', diff --git a/packages/eslint-plugin/src/configs/disable-type-checked.ts b/packages/eslint-plugin/src/configs/disable-type-checked.ts index 392e01ce304..fd3a8c2d7cb 100644 --- a/packages/eslint-plugin/src/configs/disable-type-checked.ts +++ b/packages/eslint-plugin/src/configs/disable-type-checked.ts @@ -41,6 +41,7 @@ export = { '@typescript-eslint/no-unsafe-unary-minus': 'off', '@typescript-eslint/no-useless-template-literals': 'off', '@typescript-eslint/non-nullable-type-assertion-style': 'off', + '@typescript-eslint/only-throw-error': 'off', '@typescript-eslint/prefer-destructuring': 'off', '@typescript-eslint/prefer-find': 'off', '@typescript-eslint/prefer-includes': 'off', diff --git a/packages/eslint-plugin/src/configs/strict-type-checked-only.ts b/packages/eslint-plugin/src/configs/strict-type-checked-only.ts index 7f60694d10a..12709933dfb 100644 --- a/packages/eslint-plugin/src/configs/strict-type-checked-only.ts +++ b/packages/eslint-plugin/src/configs/strict-type-checked-only.ts @@ -23,8 +23,6 @@ export = { '@typescript-eslint/no-misused-promises': 'error', '@typescript-eslint/no-mixed-enums': 'error', '@typescript-eslint/no-redundant-type-constituents': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error', '@typescript-eslint/no-unnecessary-condition': 'error', '@typescript-eslint/no-unnecessary-type-arguments': 'error', @@ -36,6 +34,8 @@ export = { '@typescript-eslint/no-unsafe-member-access': 'error', '@typescript-eslint/no-unsafe-return': 'error', '@typescript-eslint/no-useless-template-literals': 'error', + 'no-throw-literal': 'off', + '@typescript-eslint/only-throw-error': 'error', '@typescript-eslint/prefer-includes': 'error', 'prefer-promise-reject-errors': 'off', '@typescript-eslint/prefer-promise-reject-errors': 'error', diff --git a/packages/eslint-plugin/src/configs/strict-type-checked.ts b/packages/eslint-plugin/src/configs/strict-type-checked.ts index d56d87d4ca3..26d8d969881 100644 --- a/packages/eslint-plugin/src/configs/strict-type-checked.ts +++ b/packages/eslint-plugin/src/configs/strict-type-checked.ts @@ -44,8 +44,6 @@ export = { '@typescript-eslint/no-non-null-assertion': 'error', '@typescript-eslint/no-redundant-type-constituents': 'error', '@typescript-eslint/no-this-alias': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error', '@typescript-eslint/no-unnecessary-condition': 'error', '@typescript-eslint/no-unnecessary-type-arguments': 'error', @@ -64,6 +62,8 @@ export = { '@typescript-eslint/no-useless-constructor': 'error', '@typescript-eslint/no-useless-template-literals': 'error', '@typescript-eslint/no-var-requires': 'error', + 'no-throw-literal': 'off', + '@typescript-eslint/only-throw-error': 'error', '@typescript-eslint/prefer-as-const': 'error', '@typescript-eslint/prefer-includes': 'error', '@typescript-eslint/prefer-literal-enum-member': 'error', diff --git a/packages/eslint-plugin/src/rules/index.ts b/packages/eslint-plugin/src/rules/index.ts index 3534b6780c2..64c6a872ed6 100644 --- a/packages/eslint-plugin/src/rules/index.ts +++ b/packages/eslint-plugin/src/rules/index.ts @@ -101,6 +101,7 @@ import noUselessTemplateLiterals from './no-useless-template-literals'; import noVarRequires from './no-var-requires'; import nonNullableTypeAssertionStyle from './non-nullable-type-assertion-style'; import objectCurlySpacing from './object-curly-spacing'; +import onlyThrowError from './only-throw-error'; import paddingLineBetweenStatements from './padding-line-between-statements'; import parameterProperties from './parameter-properties'; import preferAsConst from './prefer-as-const'; @@ -245,6 +246,7 @@ export default { 'no-var-requires': noVarRequires, 'non-nullable-type-assertion-style': nonNullableTypeAssertionStyle, 'object-curly-spacing': objectCurlySpacing, + 'only-throw-error': onlyThrowError, 'padding-line-between-statements': paddingLineBetweenStatements, 'parameter-properties': parameterProperties, 'prefer-as-const': preferAsConst, diff --git a/packages/eslint-plugin/src/rules/no-throw-literal.ts b/packages/eslint-plugin/src/rules/no-throw-literal.ts index f3f5937c737..d893dbc164a 100644 --- a/packages/eslint-plugin/src/rules/no-throw-literal.ts +++ b/packages/eslint-plugin/src/rules/no-throw-literal.ts @@ -23,9 +23,10 @@ export default createRule({ name: 'no-throw-literal', meta: { type: 'problem', + deprecated: true, + replacedBy: ['@typescript-eslint/only-throw-error'], docs: { description: 'Disallow throwing literals as exceptions', - recommended: 'strict', extendsBaseRule: true, requiresTypeChecking: true, }, diff --git a/packages/eslint-plugin/src/rules/only-throw-error.ts b/packages/eslint-plugin/src/rules/only-throw-error.ts new file mode 100644 index 00000000000..62ce268fc70 --- /dev/null +++ b/packages/eslint-plugin/src/rules/only-throw-error.ts @@ -0,0 +1,98 @@ +import type { TSESTree } from '@typescript-eslint/utils'; +import { AST_NODE_TYPES } from '@typescript-eslint/utils'; +import * as ts from 'typescript'; + +import { + createRule, + getParserServices, + isErrorLike, + isTypeAnyType, + isTypeUnknownType, +} from '../util'; + +type MessageIds = 'object' | 'undef'; + +type Options = [ + { + allowThrowingAny?: boolean; + allowThrowingUnknown?: boolean; + }, +]; + +export default createRule({ + name: 'only-throw-error', + meta: { + type: 'problem', + docs: { + description: 'Disallow throwing non-`Error` values as exceptions', + recommended: 'strict', + extendsBaseRule: 'no-throw-literal', + requiresTypeChecking: true, + }, + schema: [ + { + type: 'object', + properties: { + allowThrowingAny: { + type: 'boolean', + }, + allowThrowingUnknown: { + type: 'boolean', + }, + }, + additionalProperties: false, + }, + ], + messages: { + object: 'Expected an error object to be thrown.', + undef: 'Do not throw undefined.', + }, + }, + defaultOptions: [ + { + allowThrowingAny: true, + allowThrowingUnknown: true, + }, + ], + create(context, [options]) { + const services = getParserServices(context); + + function checkThrowArgument(node: TSESTree.Node): void { + if ( + node.type === AST_NODE_TYPES.AwaitExpression || + node.type === AST_NODE_TYPES.YieldExpression + ) { + return; + } + + const type = services.getTypeAtLocation(node); + + if (type.flags & ts.TypeFlags.Undefined) { + context.report({ node, messageId: 'undef' }); + return; + } + + if (options.allowThrowingAny && isTypeAnyType(type)) { + return; + } + + if (options.allowThrowingUnknown && isTypeUnknownType(type)) { + return; + } + + if (isErrorLike(services.program, type)) { + return; + } + + context.report({ node, messageId: 'object' }); + } + + return { + ThrowStatement(node): void { + if (node.argument) { + checkThrowArgument(node.argument); + } + }, + }; + }, +}); diff --git a/packages/eslint-plugin/tests/rules/only-throw-error.test.ts b/packages/eslint-plugin/tests/rules/only-throw-error.test.ts new file mode 100644 index 00000000000..57fa09d86b5 --- /dev/null +++ b/packages/eslint-plugin/tests/rules/only-throw-error.test.ts @@ -0,0 +1,473 @@ +import { RuleTester } from '@typescript-eslint/rule-tester'; + +import rule from '../../src/rules/only-throw-error'; +import { getFixturesRootDir } from '../RuleTester'; + +const ruleTester = new RuleTester({ + parserOptions: { + ecmaVersion: 2018, + sourceType: 'module', + tsconfigRootDir: getFixturesRootDir(), + project: './tsconfig.json', + }, + parser: '@typescript-eslint/parser', +}); + +ruleTester.run('only-throw-error', rule, { + valid: [ + 'throw new Error();', + "throw new Error('error');", + "throw Error('error');", + ` +const e = new Error(); +throw e; + `, + ` +try { + throw new Error(); +} catch (e) { + throw e; +} + `, + ` +function foo() { + return new Error(); +} +throw foo(); + `, + ` +const foo = { + bar: new Error(), +}; +throw foo.bar; + `, + ` +const foo = { + bar: new Error(), +}; + +throw foo['bar']; + `, + ` +const foo = { + bar: new Error(), +}; + +const bar = 'bar'; +throw foo[bar]; + `, + ` +class CustomError extends Error {} +throw new CustomError(); + `, + ` +class CustomError1 extends Error {} +class CustomError2 extends CustomError1 {} +throw new CustomError2(); + `, + 'throw (foo = new Error());', + 'throw (1, 2, new Error());', + "throw 'literal' && new Error();", + "throw new Error() || 'literal';", + 'throw foo ? new Error() : new Error();', + ` +function* foo() { + let index = 0; + throw yield index++; +} + `, + ` +async function foo() { + throw await bar; +} + `, + ` +import { Error } from './missing'; +throw Error; + `, + ` +class CustomError extends Error {} +throw new CustomError(); + `, + ` +class CustomError extends Error {} +throw new CustomError(); + `, + ` +class CustomError extends Error {} +throw new CustomError(); + `, + ` +function foo() { + throw Object.assign(new Error('message'), { foo: 'bar' }); +} + `, + ` +const foo: Error | SyntaxError = bar(); +function bar() { + throw foo; +} + `, + ` +declare const foo: Error | string; +throw foo as Error; + `, + 'throw new Error() as Error;', + ` +declare const nullishError: Error | undefined; +throw nullishError ?? new Error(); + `, + ` +declare const nullishError: Error | undefined; +throw nullishError || new Error(); + `, + ` +declare const nullishError: Error | undefined; +throw nullishError ? nullishError : new Error(); + `, + ` +function fun(value: any) { + throw value; +} + `, + ` +function fun(value: unknown) { + throw value; +} + `, + ], + invalid: [ + { + code: 'throw undefined;', + errors: [ + { + messageId: 'undef', + }, + ], + }, + { + code: "throw new String('');", + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: "throw 'error';", + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: 'throw 0;', + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: 'throw false;', + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: 'throw null;', + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: 'throw {};', + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: "throw 'a' + 'b';", + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +const a = ''; +throw a + 'b'; + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: "throw (foo = 'error');", + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: 'throw (new Error(), 1, 2, 3);', + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: "throw 'literal' && 'not an Error';", + errors: [{ messageId: 'object' }], + }, + { + code: "throw 'literal' || new Error();", + errors: [{ messageId: 'object' }], + }, + { + code: "throw new Error() && 'literal';", + errors: [{ messageId: 'object' }], + }, + { + code: "throw 'literal' ?? new Error();", + errors: [{ messageId: 'object' }], + }, + { + code: "throw foo ? 'not an Error' : 'literal';", + errors: [{ messageId: 'object' }], + }, + { + code: "throw foo ? new Error() : 'literal';", + errors: [{ messageId: 'object' }], + }, + { + code: "throw foo ? 'literal' : new Error();", + errors: [{ messageId: 'object' }], + }, + { + code: 'throw `${err}`;', + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +const err = 'error'; +throw err; + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +function foo(msg) {} +throw foo('error'); + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +const foo = { + msg: 'error', +}; +throw foo.msg; + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +const foo = { + msg: undefined, +}; +throw foo.msg; + `, + errors: [ + { + messageId: 'undef', + }, + ], + }, + { + code: ` +class CustomError {} +throw new CustomError(); + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +class Foo {} +class CustomError extends Foo {} +throw new CustomError(); + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +const Error = null; +throw Error; + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +import { Error } from './class'; +throw new Error(); + `, + errors: [ + { + messageId: 'object', + line: 3, + column: 7, + }, + ], + }, + { + code: ` +class CustomError extends Foo {} +throw new CustomError(); + `, + errors: [ + { + messageId: 'object', + line: 3, + column: 7, + }, + ], + }, + { + code: ` +function foo() { + const res: T; + throw res; +} + `, + errors: [ + { + messageId: 'object', + line: 4, + column: 9, + }, + ], + }, + { + code: ` +function foo(fn: () => Promise) { + const promise = fn(); + const res = promise.then(() => {}).catch(() => {}); + throw res; +} + `, + errors: [ + { + messageId: 'object', + line: 5, + column: 9, + }, + ], + }, + { + code: ` +function foo() { + throw Object.assign({ foo: 'foo' }, { bar: 'bar' }); +} + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +const foo: Error | { bar: string } = bar(); +function bar() { + throw foo; +} + `, + errors: [ + { + messageId: 'object', + }, + ], + }, + { + code: ` +declare const foo: Error | string; +throw foo as string; + `, + errors: [{ messageId: 'object' }], + }, + { + code: ` +function fun(value: any) { + throw value; +} + `, + errors: [ + { + messageId: 'object', + }, + ], + options: [ + { + allowThrowingAny: false, + }, + ], + }, + { + code: ` +function fun(value: unknown) { + throw value; +} + `, + errors: [ + { + messageId: 'object', + }, + ], + options: [ + { + allowThrowingUnknown: false, + }, + ], + }, + ], +}); diff --git a/packages/eslint-plugin/tests/schema-snapshots/only-throw-error.shot b/packages/eslint-plugin/tests/schema-snapshots/only-throw-error.shot new file mode 100644 index 00000000000..370beb0d1bd --- /dev/null +++ b/packages/eslint-plugin/tests/schema-snapshots/only-throw-error.shot @@ -0,0 +1,32 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Rule schemas should be convertible to TS types for documentation purposes only-throw-error 1`] = ` +" +# SCHEMA: + +[ + { + "additionalProperties": false, + "properties": { + "allowThrowingAny": { + "type": "boolean" + }, + "allowThrowingUnknown": { + "type": "boolean" + } + }, + "type": "object" + } +] + + +# TYPES: + +type Options = [ + { + allowThrowingAny?: boolean; + allowThrowingUnknown?: boolean; + }, +]; +" +`; diff --git a/packages/typescript-eslint/src/configs/all.ts b/packages/typescript-eslint/src/configs/all.ts index ad4a85cd524..893a6364f56 100644 --- a/packages/typescript-eslint/src/configs/all.ts +++ b/packages/typescript-eslint/src/configs/all.ts @@ -98,8 +98,6 @@ export default ( 'no-shadow': 'off', '@typescript-eslint/no-shadow': 'error', '@typescript-eslint/no-this-alias': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error', '@typescript-eslint/no-unnecessary-condition': 'error', '@typescript-eslint/no-unnecessary-qualifier': 'error', @@ -126,6 +124,8 @@ export default ( '@typescript-eslint/no-useless-template-literals': 'error', '@typescript-eslint/no-var-requires': 'error', '@typescript-eslint/non-nullable-type-assertion-style': 'error', + 'no-throw-literal': 'off', + '@typescript-eslint/only-throw-error': 'error', '@typescript-eslint/parameter-properties': 'error', '@typescript-eslint/prefer-as-const': 'error', 'prefer-destructuring': 'off', diff --git a/packages/typescript-eslint/src/configs/disable-type-checked.ts b/packages/typescript-eslint/src/configs/disable-type-checked.ts index cf6107d6c53..30187dd7f20 100644 --- a/packages/typescript-eslint/src/configs/disable-type-checked.ts +++ b/packages/typescript-eslint/src/configs/disable-type-checked.ts @@ -43,6 +43,7 @@ export default ( '@typescript-eslint/no-unsafe-unary-minus': 'off', '@typescript-eslint/no-useless-template-literals': 'off', '@typescript-eslint/non-nullable-type-assertion-style': 'off', + '@typescript-eslint/only-throw-error': 'off', '@typescript-eslint/prefer-destructuring': 'off', '@typescript-eslint/prefer-find': 'off', '@typescript-eslint/prefer-includes': 'off', diff --git a/packages/typescript-eslint/src/configs/strict-type-checked-only.ts b/packages/typescript-eslint/src/configs/strict-type-checked-only.ts index a7947878da6..ad8fb26fbfb 100644 --- a/packages/typescript-eslint/src/configs/strict-type-checked-only.ts +++ b/packages/typescript-eslint/src/configs/strict-type-checked-only.ts @@ -31,8 +31,6 @@ export default ( '@typescript-eslint/no-misused-promises': 'error', '@typescript-eslint/no-mixed-enums': 'error', '@typescript-eslint/no-redundant-type-constituents': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error', '@typescript-eslint/no-unnecessary-condition': 'error', '@typescript-eslint/no-unnecessary-type-arguments': 'error', @@ -44,6 +42,8 @@ export default ( '@typescript-eslint/no-unsafe-member-access': 'error', '@typescript-eslint/no-unsafe-return': 'error', '@typescript-eslint/no-useless-template-literals': 'error', + 'no-throw-literal': 'off', + '@typescript-eslint/only-throw-error': 'error', '@typescript-eslint/prefer-includes': 'error', 'prefer-promise-reject-errors': 'off', '@typescript-eslint/prefer-promise-reject-errors': 'error', diff --git a/packages/typescript-eslint/src/configs/strict-type-checked.ts b/packages/typescript-eslint/src/configs/strict-type-checked.ts index fa0e7af8c0b..ba6e92ae356 100644 --- a/packages/typescript-eslint/src/configs/strict-type-checked.ts +++ b/packages/typescript-eslint/src/configs/strict-type-checked.ts @@ -52,8 +52,6 @@ export default ( '@typescript-eslint/no-non-null-assertion': 'error', '@typescript-eslint/no-redundant-type-constituents': 'error', '@typescript-eslint/no-this-alias': 'error', - 'no-throw-literal': 'off', - '@typescript-eslint/no-throw-literal': 'error', '@typescript-eslint/no-unnecessary-boolean-literal-compare': 'error', '@typescript-eslint/no-unnecessary-condition': 'error', '@typescript-eslint/no-unnecessary-type-arguments': 'error', @@ -72,6 +70,8 @@ export default ( '@typescript-eslint/no-useless-constructor': 'error', '@typescript-eslint/no-useless-template-literals': 'error', '@typescript-eslint/no-var-requires': 'error', + 'no-throw-literal': 'off', + '@typescript-eslint/only-throw-error': 'error', '@typescript-eslint/prefer-as-const': 'error', '@typescript-eslint/prefer-includes': 'error', '@typescript-eslint/prefer-literal-enum-member': 'error',