diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index 6e66e70847f..caff32caf76 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -808,56 +808,44 @@ export function resolvePackageNameToPackageJson( * this list is only the set of defaults that are implicitly included. */ export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[] { - // Use explicit type list from tsconfig.json if (options.types) { - // Check if the special "*" value is present, which means "include all from typeRoots" const hasWildcard = options.types.includes("*"); - if (hasWildcard) { - // Enumerate all packages from typeRoots - const result: string[] = []; - if (host.directoryExists && host.getDirectories) { - const typeRoots = getEffectiveTypeRoots(options, host); - if (typeRoots) { - for (const root of typeRoots) { - if (host.directoryExists(root)) { - for (const typeDirectivePath of host.getDirectories(root)) { - const normalized = normalizePath(typeDirectivePath); - const packageJsonPath = combinePaths(root, normalized, "package.json"); - // `types-publisher` sometimes creates packages with `"typings": null` for packages that don't provide their own types. - // See `createNotNeededPackageJSON` in the types-publisher` repo. - // eslint-disable-next-line no-restricted-syntax - const isNotNeededPackage = host.fileExists(packageJsonPath) && (readJson(packageJsonPath, host) as PackageJson).typings === null; - if (!isNotNeededPackage) { - const baseFileName = getBaseFileName(normalized); + if (!hasWildcard) { + return options.types; + } - // At this stage, skip results with leading dot. - if (baseFileName.charCodeAt(0) !== CharacterCodes.dot) { - // Return just the type directive names - result.push(baseFileName); - } + const result: string[] = []; + if (host.directoryExists && host.getDirectories) { + const typeRoots = getEffectiveTypeRoots(options, host); + if (typeRoots) { + for (const root of typeRoots) { + if (host.directoryExists(root)) { + for (const typeDirectivePath of host.getDirectories(root)) { + const normalized = normalizePath(typeDirectivePath); + const packageJsonPath = combinePaths(root, normalized, "package.json"); + // `types-publisher` sometimes creates packages with `"typings": null` for packages that don't provide their own types. + // See `createNotNeededPackageJSON` in the types-publisher` repo. + // eslint-disable-next-line no-restricted-syntax + const isNotNeededPackage = host.fileExists(packageJsonPath) && (readJson(packageJsonPath, host) as PackageJson).typings === null; + if (!isNotNeededPackage) { + const baseFileName = getBaseFileName(normalized); + if (baseFileName.charCodeAt(0) !== CharacterCodes.dot) { + result.push(baseFileName); } } } } } } - // Add any explicitly listed types that aren't already included (and aren't the wildcard itself) - for (const type of options.types) { - if (type !== "*" && !result.includes(type)) { - result.push(type); - } + } + for (const type of options.types) { + if (type !== "*" && !result.includes(type)) { + result.push(type); } - return result; - } - else { - return options.types; } + return result; } - else { - // When types is undefined (not specified in tsconfig), default to empty array - // This is a breaking change from the previous behavior which included all @types packages - return emptyArray; - } + return emptyArray; } export interface TypeReferenceDirectiveResolutionCache extends PerDirectoryResolutionCache, NonRelativeNameResolutionCache, PackageJsonInfoCache {