diff --git a/packages/type-utils/src/TypeOrValueSpecifier.ts b/packages/type-utils/src/TypeOrValueSpecifier.ts index a926af02735e..091528c1b227 100644 --- a/packages/type-utils/src/TypeOrValueSpecifier.ts +++ b/packages/type-utils/src/TypeOrValueSpecifier.ts @@ -1,5 +1,5 @@ import path from 'path'; -import type * as ts from 'typescript'; +import * as ts from 'typescript'; interface FileSpecifier { from: 'file'; @@ -134,9 +134,23 @@ function typeDeclaredInFile( ): boolean { if (relativePath === undefined) { const cwd = program.getCurrentDirectory().toLowerCase(); - return declarationFiles.some(declaration => - declaration.fileName.toLowerCase().startsWith(cwd), + const typeRoots = ts.getEffectiveTypeRoots( + program.getCompilerOptions(), + program, ); + + return declarationFiles.some(declaration => { + if (program.isSourceFileFromExternalLibrary(declaration)) { + return false; + } + const fileName = declaration.fileName.toLowerCase(); + if (!fileName.startsWith(cwd)) { + return false; + } + return ( + typeRoots?.some(typeRoot => fileName.startsWith(typeRoot)) !== true + ); + }); } const absolutePath = path .join(program.getCurrentDirectory(), relativePath) diff --git a/packages/type-utils/tests/TypeOrValueSpecifier.test.ts b/packages/type-utils/tests/TypeOrValueSpecifier.test.ts index 4deefa9e96d1..a1a6bfc6fd04 100644 --- a/packages/type-utils/tests/TypeOrValueSpecifier.test.ts +++ b/packages/type-utils/tests/TypeOrValueSpecifier.test.ts @@ -210,6 +210,15 @@ describe('TypeOrValueSpecifier', () => { path: 'tests/fixtures/file.ts', }, ]), + ['type RegExp = {prop: string};'].map( + (test): [string, TypeOrValueSpecifier] => [ + test, + { + from: 'file', + name: 'RegExp', + }, + ], + ), ].flat(), )('matches a matching file specifier: %s', runTestPositive); @@ -234,6 +243,13 @@ describe('TypeOrValueSpecifier', () => { path: 'tests/fixtures/wrong-file.ts', }, ], + [ + 'type Test = RegExp & {prop: string};', + { + from: 'file', + name: 'RegExp', + }, + ], ])("doesn't match a mismatched file specifier: %s", runTestNegative); it.each<[string, TypeOrValueSpecifier]>([