Skip to content

Commit 94c5484

Browse files
authored
fix(type-utils): check for type parameters on isBuiltinSymbolLikeRecurser() (#10026)
* check type parameter on isBuiltinSymbolLikeRecurser * basic tests * use tsutils.isTypeParameter() instead of type.isTypeParameter()
1 parent ced951b commit 94c5484

3 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎packages/eslint-plugin/tests/rules/only-throw-error.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ function fun(value: any) {
132132
`
133133
function fun(value: unknown) {
134134
throw value;
135+
}
136+
`,
137+
`
138+
function fun<T extends Error>(t: T): void {
139+
throw t;
135140
}
136141
`,
137142
],
@@ -468,5 +473,17 @@ function fun(value: unknown) {
468473
},
469474
],
470475
},
476+
{
477+
code: `
478+
function fun<T extends number>(t: T): void {
479+
throw t;
480+
}
481+
`,
482+
errors: [
483+
{
484+
messageId: 'object',
485+
},
486+
],
487+
},
471488
],
472489
});

‎packages/eslint-plugin/tests/rules/prefer-promise-reject-errors.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,12 @@ ruleTester.run('prefer-promise-reject-errors', rule, {
275275
}
276276
}
277277
`,
278+
`
279+
declare const foo: PromiseConstructor;
280+
function fun<T extends Error>(t: T): void {
281+
foo.reject(t);
282+
}
283+
`,
278284
],
279285
invalid: [
280286
{
@@ -1443,5 +1449,23 @@ Bar.reject(5);
14431449
},
14441450
],
14451451
},
1452+
{
1453+
code: `
1454+
declare const foo: PromiseConstructor;
1455+
function fun<T extends number>(t: T): void {
1456+
foo.reject(t);
1457+
}
1458+
`,
1459+
errors: [
1460+
{
1461+
messageId: 'rejectAnError',
1462+
type: AST_NODE_TYPES.CallExpression,
1463+
line: 4,
1464+
endLine: 4,
1465+
column: 3,
1466+
endColumn: 16,
1467+
},
1468+
],
1469+
},
14461470
],
14471471
});

‎packages/type-utils/src/builtinSymbolLikes.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as tsutils from 'ts-api-utils';
12
import * as ts from 'typescript';
23

34
import { isSymbolFromDefaultLibrary } from './isSymbolFromDefaultLibrary';
@@ -158,6 +159,16 @@ export function isBuiltinSymbolLikeRecurser(
158159
isBuiltinSymbolLikeRecurser(program, t, predicate),
159160
);
160161
}
162+
// https://github.com/JoshuaKGoldberg/ts-api-utils/issues/382
163+
if ((tsutils.isTypeParameter as (type: ts.Type) => boolean)(type)) {
164+
const t = type.getConstraint();
165+
166+
if (t) {
167+
return isBuiltinSymbolLikeRecurser(program, t, predicate);
168+
}
169+
170+
return false;
171+
}
161172

162173
const predicateResult = predicate(type);
163174
if (typeof predicateResult === 'boolean') {

0 commit comments

Comments
 (0)