Skip to content

Commit 9d53c76

Browse files
authored
fix(eslint-plugin): [no-throw-literal] fix crash caused by getBaseTypes (typescript-eslint#1830)
1 parent 188b689 commit 9d53c76

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

packages/eslint-plugin/src/rules/no-throw-literal.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ export default util.createRule({
4343
}
4444
}
4545

46-
const baseTypes = checker.getBaseTypes(type as ts.InterfaceType);
47-
for (const baseType of baseTypes) {
48-
if (isErrorLike(baseType)) {
49-
return true;
46+
if (symbol.flags & (ts.SymbolFlags.Class | ts.SymbolFlags.Interface)) {
47+
for (const baseType of checker.getBaseTypes(type as ts.InterfaceType)) {
48+
if (isErrorLike(baseType)) {
49+
return true;
50+
}
5051
}
5152
}
5253

packages/eslint-plugin/tests/rules/no-throw-literal.test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,5 +321,36 @@ throw new CustomError();
321321
},
322322
],
323323
},
324+
{
325+
code: `
326+
function foo<T>() {
327+
const res: T;
328+
throw res;
329+
}
330+
`,
331+
errors: [
332+
{
333+
messageId: 'object',
334+
line: 4,
335+
column: 9,
336+
},
337+
],
338+
},
339+
{
340+
code: `
341+
function foo<T>(fn: () => Promise<T>) {
342+
const promise = fn();
343+
const res = promise.then(() => {}).catch(() => {});
344+
throw res;
345+
}
346+
`,
347+
errors: [
348+
{
349+
messageId: 'object',
350+
line: 5,
351+
column: 9,
352+
},
353+
],
354+
},
324355
],
325356
});

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy