Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
25 changes: 13 additions & 12 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,7 @@ import {
UnionType,
UnionTypeNode,
UniqueESSymbolType,
usesWildcardTypes,
usingSingleLineStringWriter,
VariableDeclaration,
VariableDeclarationList,
Expand Down Expand Up @@ -27628,27 +27629,27 @@ 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
? 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
: Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_jQuery_Try_npm_i_save_dev_types_Slashjquery;
return usesWildcardTypes(compilerOptions)
? 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
? 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
: 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;
return usesWildcardTypes(compilerOptions)
? 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
? 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
: Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_node_Try_npm_i_save_dev_types_Slashnode;
return usesWildcardTypes(compilerOptions)
? 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
? 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
: Diagnostics.Cannot_find_name_0_Do_you_need_to_install_type_definitions_for_Bun_Try_npm_i_save_dev_types_Slashbun;
return usesWildcardTypes(compilerOptions)
? 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":
case "Set":
case "Promise":
Expand Down
19 changes: 12 additions & 7 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,15 @@ import {
emptyArray,
endsWith,
ensureTrailingDirectorySeparator,
equateValues,
every,
Extension,
extensionIsTS,
fileExtensionIs,
fileExtensionIsOneOf,
filter,
firstDefined,
flatten,
forEach,
forEachAncestorDirectory,
formatMessage,
Expand Down Expand Up @@ -105,6 +107,7 @@ import {
tryExtractTSExtension,
tryGetExtensionFromPath,
tryParsePatterns,
usesWildcardTypes,
Version,
version,
versionMajorMinor,
Expand Down Expand Up @@ -803,18 +806,17 @@ export function resolvePackageNameToPackageJson(
* Given a set of options, returns the set of type directive names
* that should be included for this program automatically.
* This list could either come from the config file,
* or from enumerating the types root + initial secondary types lookup location.
* and/or from enumerating the types root + initial secondary types lookup location given "*" compat wildcard.
* More type directives might appear in the program later as a result of loading actual source files;
* 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) {
return options.types;
if (!usesWildcardTypes(options)) {
return options.types ?? [];
}

// Walk the primary type lookup locations
const result: string[] = [];
const wildcardMatches: string[] = [];
if (host.directoryExists && host.getDirectories) {
const typeRoots = getEffectiveTypeRoots(options, host);
if (typeRoots) {
Expand All @@ -833,15 +835,18 @@ export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: M
// At this stage, skip results with leading dot.
if (baseFileName.charCodeAt(0) !== CharacterCodes.dot) {
// Return just the type directive names
result.push(baseFileName);
wildcardMatches.push(baseFileName);
}
}
}
}
}
}
}
return result;

// Order potentially matters in program construction, so substitute
// in the wildcard in the position it was specified in the types array
return deduplicate(flatten(options.types.map(t => t === "*" ? wildcardMatches : t)), equateValues);
}

export interface TypeReferenceDirectiveResolutionCache extends PerDirectoryResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, NonRelativeNameResolutionCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>, PackageJsonInfoCache {
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/programDiagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
removeSuffix,
SourceFile,
TsConfigSourceFile,
usesWildcardTypes,
} from "./_namespaces/ts.js";

interface FileReasonToChainCache {
Expand Down Expand Up @@ -400,7 +401,7 @@ export function createProgramDiagnostics(getCompilerOptionsObjectLiteralSyntax:
) :
undefined;
case FileIncludeKind.AutomaticTypeDirectiveFile:
if (!options.types) return undefined;
if (usesWildcardTypes(options)) return undefined;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when matching wildcard match to point to "wildCard" or types array?

configFileNode = getOptionsSyntaxByArrayElementValue(getCompilerOptionsObjectLiteralSyntax(), "types", reason.typeReference);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
configFileNode = getOptionsSyntaxByArrayElementValue(getCompilerOptionsObjectLiteralSyntax(), "types", reason.typeReference);
configFileNode = getOptionsSyntaxByArrayElementValue(getCompilerOptionsObjectLiteralSyntax(), "types", usesWildcardTypes(options) ? "*" : reason.typeReference);

message = Diagnostics.File_is_entry_point_of_type_library_specified_here;
break;
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/resolutionCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import {
trace,
updateResolutionField,
WatchDirectoryFlags,
usesWildcardTypes,
} from "./_namespaces/ts.js";

/** @internal */
Expand Down Expand Up @@ -1667,7 +1668,7 @@ export function createResolutionCache(resolutionHost: ResolutionCacheHost, rootD
*/
function updateTypeRootsWatch() {
const options = resolutionHost.getCompilationSettings();
if (options.types) {
if (!usesWildcardTypes(options)) {
// No need to do any watch since resolution cache is going to handle the failed lookups
// for the types added by this
closeTypeRootsWatch();
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8971,6 +8971,14 @@ export function importSyntaxAffectsModuleResolution(options: CompilerOptions): b
|| getResolvePackageJsonImports(options);
}

/**
* @internal
* Returns true if this option's types array includes "*"
*/
export function usesWildcardTypes(options: CompilerOptions): options is CompilerOptions & { types: string[]; } {
return some(options.types, t => t === "*");
}

type CompilerOptionKeys = keyof { [K in keyof CompilerOptions as string extends K ? never : K]: any; };
function createComputedCompilerOptions<T extends Record<string, CompilerOptionKeys[]>>(
options: {
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import {
sourceMapCommentRegExpDontCareLineStart,
sys,
System,
usesWildcardTypes,
WatchCompilerHost,
WatchCompilerHostOfConfigFile,
WatchCompilerHostOfFilesAndCompilerOptions,
Expand Down Expand Up @@ -529,7 +530,7 @@ export function fileIncludeReasonToDiagnostics(program: Program, reason: FileInc
options.outFile ? "--outFile" : "--out",
);
case FileIncludeKind.AutomaticTypeDirectiveFile: {
const messageAndArgs: DiagnosticAndArguments = options.types ?
const messageAndArgs: DiagnosticAndArguments = !usesWildcardTypes(options) ?
reason.packageId ?
[Diagnostics.Entry_point_of_type_library_0_specified_in_compilerOptions_with_packageId_1, reason.typeReference, packageIdToString(reason.packageId)] :
[Diagnostics.Entry_point_of_type_library_0_specified_in_compilerOptions, reason.typeReference] :
Expand Down
3 changes: 2 additions & 1 deletion src/jsTyping/jsTyping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
some,
toFileNameLowerCase,
TypeAcquisition,
usesWildcardTypes,
Version,
versionMajorMinor,
} from "./_namespaces/ts.js";
Expand Down Expand Up @@ -133,7 +134,7 @@ export function discoverTypings(
const exclude = typeAcquisition.exclude || [];

// Directories to search for package.json, bower.json and other typing information
if (!compilerOptions.types) {
if (!compilerOptions.types || usesWildcardTypes(compilerOptions)) {
const possibleSearchDirs = new Set(fileNames.map(getDirectoryPath));
possibleSearchDirs.add(projectRootPath);
possibleSearchDirs.forEach(searchDir => {
Expand Down
5 changes: 3 additions & 2 deletions src/testRunner/unittests/tsbuild/moduleResolution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,13 @@ describe("unittests:: tsbuild:: moduleResolution:: handles the modules and optio
TestServerHost.createWatchedSystem({
"/home/src/workspaces/project/packages/pkg1_index.ts": `export const theNum: TheNum = "type1";`,
"/home/src/workspaces/project/packages/pkg1.tsconfig.json": jsonToReadableText({
compilerOptions: { composite: true, typeRoots: ["./typeroot1"] },
compilerOptions: { composite: true, typeRoots: ["./typeroot1"], types: ["*"] },
files: ["./pkg1_index.ts"],
}),
"/home/src/workspaces/project/packages/typeroot1/sometype/index.d.ts": dedent`declare type TheNum = "type1";`,
"/home/src/workspaces/project/packages/pkg2_index.ts": `export const theNum: TheNum2 = "type2";`,
"/home/src/workspaces/project/packages/pkg2.tsconfig.json": jsonToReadableText({
compilerOptions: { composite: true, typeRoots: ["./typeroot2"] },
compilerOptions: { composite: true, typeRoots: ["./typeroot2"], types: ["*"] },
files: ["./pkg2_index.ts"],
}),
"/home/src/workspaces/project/packages/typeroot2/sometype/index.d.ts": dedent`declare type TheNum2 = "type2";`,
Expand Down Expand Up @@ -164,6 +164,7 @@ describe("unittests:: tsbuild:: moduleResolution:: impliedNodeFormat differs bet
"/home/src/workspaces/project/a/src/index.ts": "",
"/home/src/workspaces/project/a/tsconfig.json": jsonToReadableText({
compilerOptions: { strict: true },
types: ["*"]
}),
"/home/src/workspaces/project/b/src/index.ts": dedent`
import pg from "pg";
Expand Down
4 changes: 2 additions & 2 deletions src/testRunner/unittests/tsc/incremental.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ declare global {
"/home/src/workspaces/project/node_modules/react/jsx-runtime.js": "export {}", // js needs to be present so there's a resolution result
"/home/src/workspaces/project/node_modules/@types/react/index.d.ts": getJsxLibraryContent(), // doesn't contain a jsx-runtime definition
"/home/src/workspaces/project/src/index.tsx": `export const App = () => <div propA={true}></div>;`,
"/home/src/workspaces/project/tsconfig.json": jsonToReadableText({ compilerOptions: { module: "commonjs", jsx: "react-jsx", incremental: true, jsxImportSource: "react" } }),
"/home/src/workspaces/project/tsconfig.json": jsonToReadableText({ compilerOptions: { module: "commonjs", jsx: "react-jsx", incremental: true, jsxImportSource: "react", types: ["react"] } }),
}),
commandLineArgs: ts.emptyArray,
});
Expand All @@ -201,7 +201,7 @@ declare global {
"/home/src/workspaces/project/node_modules/react/jsx-runtime.js": "export {}", // js needs to be present so there's a resolution result
"/home/src/workspaces/project/node_modules/@types/react/index.d.ts": getJsxLibraryContent(), // doesn't contain a jsx-runtime definition
"/home/src/workspaces/project/src/index.tsx": `export const App = () => <div propA={true}></div>;`,
"/home/src/workspaces/project/tsconfig.json": jsonToReadableText({ compilerOptions: { module: "commonjs", jsx: "react-jsx", incremental: true, jsxImportSource: "react" } }),
"/home/src/workspaces/project/tsconfig.json": jsonToReadableText({ compilerOptions: { module: "commonjs", jsx: "react-jsx", incremental: true, jsxImportSource: "react", types: ["react"] } }),
}),
commandLineArgs: ["--strict"],
});
Expand Down
4 changes: 2 additions & 2 deletions src/testRunner/unittests/tscWatch/resolutionCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ describe("unittests:: tscWatch:: resolutionCache:: tsc-watch module resolution c
verifyTscWatch({
scenario,
subScenario: "works when module resolution changes to ambient module",
commandLineArgs: ["-w", "/users/username/projects/project/foo.ts"],
commandLineArgs: ["-w", "/users/username/projects/project/foo.ts", "-types", "node"],
sys: () =>
TestServerHost.createWatchedSystem([{
path: "/users/username/projects/project/foo.ts",
Expand Down Expand Up @@ -565,7 +565,7 @@ declare namespace NodeJS {
};
const tsconfig: File = {
path: `/user/username/projects/myproject/tsconfig.json`,
content: "{}",
content: "{ \"compilerOptions\": { \"types\": [\"*\"] } }",
};
const { nodeAtTypesIndex, nodeAtTypesBase, nodeAtTypes36Base, nodeAtTypesGlobals } = getNodeAtTypes();
return TestServerHost.createWatchedSystem(
Expand Down
2 changes: 1 addition & 1 deletion src/testRunner/unittests/tsserver/autoImportProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const angularCorePackageJson: File = {
};
const tsconfig: File = {
path: "/user/username/projects/project/tsconfig.json",
content: `{ "compilerOptions": { "module": "commonjs" } }`,
content: `{ "compilerOptions": { "module": "commonjs", "types": ["*"] } }`,
};
const packageJson: File = {
path: "/user/username/projects/project/package.json",
Expand Down
Loading