Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/eslint-plugin/tests/rules/only-throw-error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ function fun(value: any) {
`
function fun(value: unknown) {
throw value;
}
`,
`
function fun<T extends Error>(t: T): void {
throw t;
}
`,
],
Expand Down Expand Up @@ -468,5 +473,17 @@ function fun(value: unknown) {
},
],
},
{
code: `
function fun<T extends number>(t: T): void {
throw t;
}
`,
errors: [
{
messageId: 'object',
},
],
},
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,12 @@ ruleTester.run('prefer-promise-reject-errors', rule, {
}
}
`,
`
declare const foo: PromiseConstructor;
function fun<T extends Error>(t: T): void {
foo.reject(t);
}
`,
],
invalid: [
{
Expand Down Expand Up @@ -1443,5 +1449,23 @@ Bar.reject(5);
},
],
},
{
code: `
declare const foo: PromiseConstructor;
function fun<T extends number>(t: T): void {
foo.reject(t);
}
`,
errors: [
{
messageId: 'rejectAnError',
type: AST_NODE_TYPES.CallExpression,
line: 4,
endLine: 4,
column: 3,
endColumn: 16,
},
],
},
],
});
11 changes: 11 additions & 0 deletions packages/type-utils/src/builtinSymbolLikes.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as tsutils from 'ts-api-utils';
import * as ts from 'typescript';

import { isSymbolFromDefaultLibrary } from './isSymbolFromDefaultLibrary';
Expand Down Expand Up @@ -158,6 +159,16 @@ export function isBuiltinSymbolLikeRecurser(
isBuiltinSymbolLikeRecurser(program, t, predicate),
);
}
// https://github.com/JoshuaKGoldberg/ts-api-utils/issues/382
if ((tsutils.isTypeParameter as (type: ts.Type) => boolean)(type)) {
const t = type.getConstraint();

if (t) {
return isBuiltinSymbolLikeRecurser(program, t, predicate);
}

return false;
}

const predicateResult = predicate(type);
if (typeof predicateResult === 'boolean') {
Expand Down