diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 92898f8b4f8..3d8d6c7dcd2 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1113,6 +1113,7 @@ import { TypeReferenceNode, TypeReferenceSerializationKind, TypeReferenceType, + typesIncludesWildcard, TypeVariable, unescapeLeadingUnderscores, UnionOrIntersectionType, @@ -27628,25 +27629,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { case "console": return Diagnostics.Cannot_find_name_0_Do_you_need_to_change_your_target_library_Try_changing_the_lib_compiler_option_to_include_dom; case "$": - return compilerOptions.types?.includes("*") + return typesIncludesWildcard(compilerOptions.types) ? Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery : Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery_and_then_add_jquery_to_the_types_field_in_your_tsconfig; case "describe": case "suite": case "it": case "test": - return compilerOptions.types?.includes("*") + return typesIncludesWildcard(compilerOptions.types) ? Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_save_dev_types_Slashjest_or_npm_i_save_dev_types_Slashmocha : Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_a_test_runner_Try_npm_i_save_dev_types_Slashjest_or_npm_i_save_dev_types_Slashmocha_and_then_add_jest_or_mocha_to_the_types_field_in_your_tsconfig; case "process": case "require": case "Buffer": case "module": - return compilerOptions.types?.includes("*") + return typesIncludesWildcard(compilerOptions.types) ? Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode : Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode_and_then_add_node_to_the_types_field_in_your_tsconfig; case "Bun": - return compilerOptions.types?.includes("*") + return typesIncludesWildcard(compilerOptions.types) ? Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_Bun_Try_npm_i_save_dev_types_Slashbun : Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_Bun_Try_npm_i_save_dev_types_Slashbun_and_then_add_bun_to_the_types_field_in_your_tsconfig; case "Map": diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index 86355df079b..9dcf70a4caf 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -801,6 +801,14 @@ export function resolvePackageNameToPackageJson( }); } +/** + * Returns true if the types compiler option includes the "*" wildcard. + * @internal + */ +export function typesIncludesWildcard(types: readonly string[] | undefined): boolean { + return types?.includes("*") ?? false; +} + /** * Given a set of options, returns the set of type directive names * that should be included for this program automatically. @@ -815,7 +823,7 @@ export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: M return emptyArray; } - if (!options.types.includes("*")) { + if (!typesIncludesWildcard(options.types)) { // No wildcard, no need to iterate anything return options.types; } diff --git a/src/compiler/programDiagnostics.ts b/src/compiler/programDiagnostics.ts index ead43fb745d..816fb177c34 100644 --- a/src/compiler/programDiagnostics.ts +++ b/src/compiler/programDiagnostics.ts @@ -52,6 +52,7 @@ import { removeSuffix, SourceFile, TsConfigSourceFile, + typesIncludesWildcard, } from "./_namespaces/ts.js"; interface FileReasonToChainCache { @@ -400,7 +401,7 @@ export function createProgramDiagnostics(getCompilerOptionsObjectLiteralSyntax: ) : undefined; case FileIncludeKind.AutomaticTypeDirectiveFile: - if (options.types?.includes("*")) return undefined; + if (typesIncludesWildcard(options.types)) return undefined; configFileNode = getOptionsSyntaxByArrayElementValue(getCompilerOptionsObjectLiteralSyntax(), "types", reason.typeReference); message = Diagnostics.File_is_entry_point_of_type_library_specified_here; break; diff --git a/src/compiler/resolutionCache.ts b/src/compiler/resolutionCache.ts index 4545b880907..32739fb2a73 100644 --- a/src/compiler/resolutionCache.ts +++ b/src/compiler/resolutionCache.ts @@ -73,6 +73,7 @@ import { startsWith, StringLiteralLike, trace, + typesIncludesWildcard, updateResolutionField, WatchDirectoryFlags, } from "./_namespaces/ts.js"; @@ -1667,7 +1668,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD */ function updateTypeRootsWatch() { const options = resolutionHost.getCompilationSettings(); - if (options.types && !options.types.includes("*")) { + if (options.types && !typesIncludesWildcard(options.types)) { // No need to do any watch since resolution cache is going to handle the failed lookups // for the types added by this closeTypeRootsWatch(); diff --git a/src/compiler/watch.ts b/src/compiler/watch.ts index ee5905a336a..53b4c6ee56c 100644 --- a/src/compiler/watch.ts +++ b/src/compiler/watch.ts @@ -97,6 +97,7 @@ import { sourceMapCommentRegExpDontCareLineStart, sys, System, + typesIncludesWildcard, WatchCompilerHost, WatchCompilerHostOfConfigFile, WatchCompilerHostOfFilesAndCompilerOptions, @@ -529,7 +530,7 @@ export function fileIncludeReasonToDiagnostics(program: Program, reason: FileInc options.outFile ? "--outFile" : "--out", ); case FileIncludeKind.AutomaticTypeDirectiveFile: { - const messageAndArgs: DiagnosticAndArguments = options.types?.includes("*") ? + const messageAndArgs: DiagnosticAndArguments = typesIncludesWildcard(options.types) ? reason.packageId ? [Diagnostics.Entry_point_for_implicit_type_library_0_with_packageId_1, reason.typeReference, packageIdToString(reason.packageId)] : [Diagnostics.Entry_point_for_implicit_type_library_0, reason.typeReference] : diff --git a/src/jsTyping/jsTyping.ts b/src/jsTyping/jsTyping.ts index 8278c5929c6..7f83a77beec 100644 --- a/src/jsTyping/jsTyping.ts +++ b/src/jsTyping/jsTyping.ts @@ -28,6 +28,7 @@ import { some, toFileNameLowerCase, TypeAcquisition, + typesIncludesWildcard, Version, versionMajorMinor, } from "./_namespaces/ts.js"; @@ -133,8 +134,7 @@ export function discoverTypings( const exclude = typeAcquisition.exclude || []; // Directories to search for package.json, bower.json and other typing information - if (compilerOptions.types?.includes("*")) { - + if (typesIncludesWildcard(compilerOptions.types)) { const possibleSearchDirs = new Set(fileNames.map(getDirectoryPath)); possibleSearchDirs.add(projectRootPath); possibleSearchDirs.forEach(searchDir => {