diff --git a/packages/eslint-plugin/tests/rules/only-throw-error.test.ts b/packages/eslint-plugin/tests/rules/only-throw-error.test.ts index 6f60f01b840..ee9d196be63 100644 --- a/packages/eslint-plugin/tests/rules/only-throw-error.test.ts +++ b/packages/eslint-plugin/tests/rules/only-throw-error.test.ts @@ -191,6 +191,19 @@ throw new Map(); }, { code: ` +function func() { + let err: Promise | Promise; + throw err; +} + `, + options: [ + { + allow: ['Promise'], + }, + ], + }, + { + code: ` try { } catch (e) { throw e; diff --git a/packages/type-utils/src/TypeOrValueSpecifier.ts b/packages/type-utils/src/TypeOrValueSpecifier.ts index bbfb99ddcc7..9ef6c91dc9b 100644 --- a/packages/type-utils/src/TypeOrValueSpecifier.ts +++ b/packages/type-utils/src/TypeOrValueSpecifier.ts @@ -169,6 +169,10 @@ export function typeMatchesSpecifier( specifier: TypeOrValueSpecifier, program: ts.Program, ): boolean { + if (tsutils.isUnionType(type)) { + return type.types.some(t => typeMatchesSpecifier(t, specifier, program)); + } + const wholeTypeMatches = ((): boolean => { if (tsutils.isIntrinsicErrorType(type)) { return false; diff --git a/packages/type-utils/tests/TypeOrValueSpecifier.test.ts b/packages/type-utils/tests/TypeOrValueSpecifier.test.ts index a28d22f36b7..05a4ad1a8f2 100644 --- a/packages/type-utils/tests/TypeOrValueSpecifier.test.ts +++ b/packages/type-utils/tests/TypeOrValueSpecifier.test.ts @@ -167,6 +167,7 @@ describe('TypeOrValueSpecifier', () => { it.for([ ['interface Foo {prop: string}; type Test = Foo;', 'Foo'], ['type Test = RegExp;', 'RegExp'], + ['type Test = RegExp | BigInt;', 'RegExp'], ] as const satisfies [string, TypeOrValueSpecifier][])( 'matches a matching universal string specifier: %s\n\t%s', ([code, typeOrValueSpecifier], { expect }) => { @@ -191,6 +192,10 @@ describe('TypeOrValueSpecifier', () => { 'interface Foo {prop: string}; type Test = Foo;', { from: 'file', name: 'Foo' }, ], + [ + 'interface Foo {prop: string}; type Test = Foo | number;', + { from: 'file', name: 'Foo' }, + ], [ 'type Foo = {prop: string}; type Test = Foo;', { from: 'file', name: 'Foo' }, @@ -296,6 +301,7 @@ describe('TypeOrValueSpecifier', () => { it.for([ ['type Test = RegExp;', { from: 'lib', name: 'RegExp' }], + ['type Test = RegExp | BigInt;', { from: 'lib', name: 'RegExp' }], ['type Test = RegExp;', { from: 'lib', name: ['RegExp', 'BigInt'] }], ] as const satisfies [string, TypeOrValueSpecifier][])( 'matches a matching lib specifier: %s\n\t%s', @@ -316,6 +322,7 @@ describe('TypeOrValueSpecifier', () => { it.for([ ['type Test = string;', { from: 'lib', name: 'string' }], + ['type Test = string | number;', { from: 'lib', name: 'string' }], ['type Test = string;', { from: 'lib', name: ['string', 'number'] }], ] as const satisfies [string, TypeOrValueSpecifier][])( 'matches a matching intrinsic type specifier: %s\n\t%s', @@ -339,6 +346,10 @@ describe('TypeOrValueSpecifier', () => { 'import type {Node} from "typescript"; type Test = Node;', { from: 'package', name: 'Node', package: 'typescript' }, ], + [ + 'import type {Node} from "typescript"; type Test = Node | Symbol;', + { from: 'package', name: 'Node', package: 'typescript' }, + ], [ 'import type {Node} from "typescript"; type Test = Node;', { from: 'package', name: ['Node', 'Symbol'], package: 'typescript' },