diff --git a/packages/eslint-plugin/src/rules/no-throw-literal.ts b/packages/eslint-plugin/src/rules/no-throw-literal.ts index 67ba9e52ebe..b8017c4483a 100644 --- a/packages/eslint-plugin/src/rules/no-throw-literal.ts +++ b/packages/eslint-plugin/src/rules/no-throw-literal.ts @@ -66,7 +66,8 @@ export default util.createRule({ case AST_NODE_TYPES.Identifier: case AST_NODE_TYPES.CallExpression: case AST_NODE_TYPES.NewExpression: - case AST_NODE_TYPES.MemberExpression: { + case AST_NODE_TYPES.MemberExpression: + case AST_NODE_TYPES.TSAsExpression: { const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node); return checker.getTypeAtLocation(tsNode); } diff --git a/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts b/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts index 09ecadeb629..883894bf2a1 100644 --- a/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts +++ b/packages/eslint-plugin/tests/rules/no-throw-literal.test.ts @@ -101,14 +101,17 @@ function foo() { throw Object.assign(new Error('message'), { foo: 'bar' }); } `, - { - code: ` + ` const foo: Error | SyntaxError = bar(); function bar() { throw foo; } - `, - }, + `, + ` +declare const foo: Error | string; +throw foo as Error; + `, + 'throw new Error() as Error;', ], invalid: [ { @@ -390,5 +393,12 @@ function bar() { }, ], }, + { + code: ` +declare const foo: Error | string; +throw foo as string; + `, + errors: [{ messageId: 'object' }], + }, ], });