From 2b70067c56b783bc5e4848e75d586335e3477548 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 21 Jan 2026 23:18:01 +0000 Subject: [PATCH] Implement default types=[] and wildcard support - Modified getAutomaticTypeDirectiveNames to return empty array when types is undefined - Added support for "*" wildcard value to opt into old "include all" behavior - Created comprehensive test cases for new functionality Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com> --- src/compiler/moduleNameResolver.ts | 13 +++++++- .../reference/typesOptionDefaultEmpty.js | 18 ++++++++++ .../reference/typesOptionDefaultEmpty.symbols | 8 +++++ .../typesOptionDefaultEmpty.trace.json | 1 + .../reference/typesOptionDefaultEmpty.types | 11 +++++++ .../typesOptionExplicitList.errors.txt | 21 ++++++++++++ .../reference/typesOptionExplicitList.js | 21 ++++++++++++ .../reference/typesOptionExplicitList.symbols | 17 ++++++++++ .../typesOptionExplicitList.trace.json | 12 +++++++ .../reference/typesOptionExplicitList.types | 28 ++++++++++++++++ .../reference/typesOptionWildcard.js | 20 +++++++++++ .../reference/typesOptionWildcard.symbols | 25 ++++++++++++++ .../reference/typesOptionWildcard.trace.json | 22 +++++++++++++ .../reference/typesOptionWildcard.types | 33 +++++++++++++++++++ .../cases/compiler/typesOptionDefaultEmpty.ts | 17 ++++++++++ .../cases/compiler/typesOptionExplicitList.ts | 19 +++++++++++ tests/cases/compiler/typesOptionWildcard.ts | 18 ++++++++++ 17 files changed, 303 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/typesOptionDefaultEmpty.js create mode 100644 tests/baselines/reference/typesOptionDefaultEmpty.symbols create mode 100644 tests/baselines/reference/typesOptionDefaultEmpty.trace.json create mode 100644 tests/baselines/reference/typesOptionDefaultEmpty.types create mode 100644 tests/baselines/reference/typesOptionExplicitList.errors.txt create mode 100644 tests/baselines/reference/typesOptionExplicitList.js create mode 100644 tests/baselines/reference/typesOptionExplicitList.symbols create mode 100644 tests/baselines/reference/typesOptionExplicitList.trace.json create mode 100644 tests/baselines/reference/typesOptionExplicitList.types create mode 100644 tests/baselines/reference/typesOptionWildcard.js create mode 100644 tests/baselines/reference/typesOptionWildcard.symbols create mode 100644 tests/baselines/reference/typesOptionWildcard.trace.json create mode 100644 tests/baselines/reference/typesOptionWildcard.types create mode 100644 tests/cases/compiler/typesOptionDefaultEmpty.ts create mode 100644 tests/cases/compiler/typesOptionExplicitList.ts create mode 100644 tests/cases/compiler/typesOptionWildcard.ts diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index d665072ae51..eb839dbea6f 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -810,7 +810,18 @@ export function resolvePackageNameToPackageJson( export function getAutomaticTypeDirectiveNames(options: CompilerOptions, host: ModuleResolutionHost): string[] { // Use explicit type list from tsconfig.json if (options.types) { - return options.types; + // Check if the special "*" value is present, which means "include all" + if (options.types.length === 1 && options.types[0] === "*") { + // Fall through to enumerate all packages from typeRoots + } + else { + return options.types; + } + } + 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; } // Walk the primary type lookup locations diff --git a/tests/baselines/reference/typesOptionDefaultEmpty.js b/tests/baselines/reference/typesOptionDefaultEmpty.js new file mode 100644 index 00000000000..fd82302b500 --- /dev/null +++ b/tests/baselines/reference/typesOptionDefaultEmpty.js @@ -0,0 +1,18 @@ +//// [tests/cases/compiler/typesOptionDefaultEmpty.ts] //// + +//// [index.d.ts] +declare var $: { x: number }; + +//// [index.d.ts] +declare var _: { map: any }; + +//// [app.ts] +// With the new default behavior, @types packages are not automatically included +// unless explicitly listed in the types array or imported +const value = 42; + + +//// [app.js] +// With the new default behavior, @types packages are not automatically included +// unless explicitly listed in the types array or imported +var value = 42; diff --git a/tests/baselines/reference/typesOptionDefaultEmpty.symbols b/tests/baselines/reference/typesOptionDefaultEmpty.symbols new file mode 100644 index 00000000000..0a4ebc7a8da --- /dev/null +++ b/tests/baselines/reference/typesOptionDefaultEmpty.symbols @@ -0,0 +1,8 @@ +//// [tests/cases/compiler/typesOptionDefaultEmpty.ts] //// + +=== /app.ts === +// With the new default behavior, @types packages are not automatically included +// unless explicitly listed in the types array or imported +const value = 42; +>value : Symbol(value, Decl(app.ts, 2, 5)) + diff --git a/tests/baselines/reference/typesOptionDefaultEmpty.trace.json b/tests/baselines/reference/typesOptionDefaultEmpty.trace.json new file mode 100644 index 00000000000..0637a088a01 --- /dev/null +++ b/tests/baselines/reference/typesOptionDefaultEmpty.trace.json @@ -0,0 +1 @@ +[] \ No newline at end of file diff --git a/tests/baselines/reference/typesOptionDefaultEmpty.types b/tests/baselines/reference/typesOptionDefaultEmpty.types new file mode 100644 index 00000000000..0acb268826c --- /dev/null +++ b/tests/baselines/reference/typesOptionDefaultEmpty.types @@ -0,0 +1,11 @@ +//// [tests/cases/compiler/typesOptionDefaultEmpty.ts] //// + +=== /app.ts === +// With the new default behavior, @types packages are not automatically included +// unless explicitly listed in the types array or imported +const value = 42; +>value : 42 +> : ^^ +>42 : 42 +> : ^^ + diff --git a/tests/baselines/reference/typesOptionExplicitList.errors.txt b/tests/baselines/reference/typesOptionExplicitList.errors.txt new file mode 100644 index 00000000000..a927c4e2629 --- /dev/null +++ b/tests/baselines/reference/typesOptionExplicitList.errors.txt @@ -0,0 +1,21 @@ +/app.ts(5,1): error TS2304: Cannot find name '_'. + + +==== /tsconfig.json (0 errors) ==== + { "compilerOptions": { "types": ["jquery"] } } + +==== /app.ts (1 errors) ==== + // With "types": ["jquery"], only jquery is included + $.x; + + // lodash is not included, so this should error + _.map; + ~ +!!! error TS2304: Cannot find name '_'. + +==== /node_modules/@types/jquery/index.d.ts (0 errors) ==== + declare var $: { x: number }; + +==== /node_modules/@types/lodash/index.d.ts (0 errors) ==== + declare var _: { map: any }; + \ No newline at end of file diff --git a/tests/baselines/reference/typesOptionExplicitList.js b/tests/baselines/reference/typesOptionExplicitList.js new file mode 100644 index 00000000000..0afd96752bc --- /dev/null +++ b/tests/baselines/reference/typesOptionExplicitList.js @@ -0,0 +1,21 @@ +//// [tests/cases/compiler/typesOptionExplicitList.ts] //// + +//// [index.d.ts] +declare var $: { x: number }; + +//// [index.d.ts] +declare var _: { map: any }; + +//// [app.ts] +// With "types": ["jquery"], only jquery is included +$.x; + +// lodash is not included, so this should error +_.map; + + +//// [app.js] +// With "types": ["jquery"], only jquery is included +$.x; +// lodash is not included, so this should error +_.map; diff --git a/tests/baselines/reference/typesOptionExplicitList.symbols b/tests/baselines/reference/typesOptionExplicitList.symbols new file mode 100644 index 00000000000..bfaa0fc5dd5 --- /dev/null +++ b/tests/baselines/reference/typesOptionExplicitList.symbols @@ -0,0 +1,17 @@ +//// [tests/cases/compiler/typesOptionExplicitList.ts] //// + +=== /app.ts === +// With "types": ["jquery"], only jquery is included +$.x; +>$.x : Symbol(x, Decl(index.d.ts, 0, 16)) +>$ : Symbol($, Decl(index.d.ts, 0, 11)) +>x : Symbol(x, Decl(index.d.ts, 0, 16)) + +// lodash is not included, so this should error +_.map; + +=== /node_modules/@types/jquery/index.d.ts === +declare var $: { x: number }; +>$ : Symbol($, Decl(index.d.ts, 0, 11)) +>x : Symbol(x, Decl(index.d.ts, 0, 16)) + diff --git a/tests/baselines/reference/typesOptionExplicitList.trace.json b/tests/baselines/reference/typesOptionExplicitList.trace.json new file mode 100644 index 00000000000..53227101f7d --- /dev/null +++ b/tests/baselines/reference/typesOptionExplicitList.trace.json @@ -0,0 +1,12 @@ +[ + "======== Resolving type reference directive 'jquery', containing file '/__inferred type names__.ts', root directory '/node_modules/@types'. ========", + "Resolving with primary search path '/node_modules/@types'.", + "File '/node_modules/@types/jquery/package.json' does not exist.", + "File '/node_modules/@types/jquery/index.d.ts' exists - use it as a name resolution result.", + "Resolving real path for '/node_modules/@types/jquery/index.d.ts', result '/node_modules/@types/jquery/index.d.ts'.", + "======== Type reference directive 'jquery' was successfully resolved to '/node_modules/@types/jquery/index.d.ts', primary: true. ========", + "File '/node_modules/@types/jquery/package.json' does not exist according to earlier cached lookups.", + "File '/node_modules/@types/package.json' does not exist.", + "File '/node_modules/package.json' does not exist.", + "File '/package.json' does not exist." +] \ No newline at end of file diff --git a/tests/baselines/reference/typesOptionExplicitList.types b/tests/baselines/reference/typesOptionExplicitList.types new file mode 100644 index 00000000000..3379d52c791 --- /dev/null +++ b/tests/baselines/reference/typesOptionExplicitList.types @@ -0,0 +1,28 @@ +//// [tests/cases/compiler/typesOptionExplicitList.ts] //// + +=== /app.ts === +// With "types": ["jquery"], only jquery is included +$.x; +>$.x : number +> : ^^^^^^ +>$ : { x: number; } +> : ^^^^^ ^^^ +>x : number +> : ^^^^^^ + +// lodash is not included, so this should error +_.map; +>_.map : any +> : ^^^ +>_ : any +> : ^^^ +>map : any +> : ^^^ + +=== /node_modules/@types/jquery/index.d.ts === +declare var $: { x: number }; +>$ : { x: number; } +> : ^^^^^ ^^^ +>x : number +> : ^^^^^^ + diff --git a/tests/baselines/reference/typesOptionWildcard.js b/tests/baselines/reference/typesOptionWildcard.js new file mode 100644 index 00000000000..3c9ceef04b3 --- /dev/null +++ b/tests/baselines/reference/typesOptionWildcard.js @@ -0,0 +1,20 @@ +//// [tests/cases/compiler/typesOptionWildcard.ts] //// + +//// [index.d.ts] +declare var $: { x: number }; + +//// [index.d.ts] +declare var _: { map: any }; + +//// [app.ts] +// With "types": ["*"], all @types packages are automatically included +// This is the opt-in to the old behavior +$.x; +_.map; + + +//// [app.js] +// With "types": ["*"], all @types packages are automatically included +// This is the opt-in to the old behavior +$.x; +_.map; diff --git a/tests/baselines/reference/typesOptionWildcard.symbols b/tests/baselines/reference/typesOptionWildcard.symbols new file mode 100644 index 00000000000..4d31a30ec7d --- /dev/null +++ b/tests/baselines/reference/typesOptionWildcard.symbols @@ -0,0 +1,25 @@ +//// [tests/cases/compiler/typesOptionWildcard.ts] //// + +=== /app.ts === +// With "types": ["*"], all @types packages are automatically included +// This is the opt-in to the old behavior +$.x; +>$.x : Symbol(x, Decl(index.d.ts, 0, 16)) +>$ : Symbol($, Decl(index.d.ts, 0, 11)) +>x : Symbol(x, Decl(index.d.ts, 0, 16)) + +_.map; +>_.map : Symbol(map, Decl(index.d.ts, 0, 16)) +>_ : Symbol(_, Decl(index.d.ts, 0, 11)) +>map : Symbol(map, Decl(index.d.ts, 0, 16)) + +=== /node_modules/@types/jquery/index.d.ts === +declare var $: { x: number }; +>$ : Symbol($, Decl(index.d.ts, 0, 11)) +>x : Symbol(x, Decl(index.d.ts, 0, 16)) + +=== /node_modules/@types/lodash/index.d.ts === +declare var _: { map: any }; +>_ : Symbol(_, Decl(index.d.ts, 0, 11)) +>map : Symbol(map, Decl(index.d.ts, 0, 16)) + diff --git a/tests/baselines/reference/typesOptionWildcard.trace.json b/tests/baselines/reference/typesOptionWildcard.trace.json new file mode 100644 index 00000000000..1934501dac3 --- /dev/null +++ b/tests/baselines/reference/typesOptionWildcard.trace.json @@ -0,0 +1,22 @@ +[ + "======== Resolving type reference directive 'jquery', containing file '/__inferred type names__.ts', root directory '/node_modules/@types'. ========", + "Resolving with primary search path '/node_modules/@types'.", + "File '/node_modules/@types/jquery/package.json' does not exist.", + "File '/node_modules/@types/jquery/index.d.ts' exists - use it as a name resolution result.", + "Resolving real path for '/node_modules/@types/jquery/index.d.ts', result '/node_modules/@types/jquery/index.d.ts'.", + "======== Type reference directive 'jquery' was successfully resolved to '/node_modules/@types/jquery/index.d.ts', primary: true. ========", + "======== Resolving type reference directive 'lodash', containing file '/__inferred type names__.ts', root directory '/node_modules/@types'. ========", + "Resolving with primary search path '/node_modules/@types'.", + "File '/node_modules/@types/lodash/package.json' does not exist.", + "File '/node_modules/@types/lodash/index.d.ts' exists - use it as a name resolution result.", + "Resolving real path for '/node_modules/@types/lodash/index.d.ts', result '/node_modules/@types/lodash/index.d.ts'.", + "======== Type reference directive 'lodash' was successfully resolved to '/node_modules/@types/lodash/index.d.ts', primary: true. ========", + "File '/node_modules/@types/jquery/package.json' does not exist according to earlier cached lookups.", + "File '/node_modules/@types/package.json' does not exist.", + "File '/node_modules/package.json' does not exist.", + "File '/package.json' does not exist.", + "File '/node_modules/@types/lodash/package.json' does not exist according to earlier cached lookups.", + "File '/node_modules/@types/package.json' does not exist according to earlier cached lookups.", + "File '/node_modules/package.json' does not exist according to earlier cached lookups.", + "File '/package.json' does not exist according to earlier cached lookups." +] \ No newline at end of file diff --git a/tests/baselines/reference/typesOptionWildcard.types b/tests/baselines/reference/typesOptionWildcard.types new file mode 100644 index 00000000000..37fcf5c0d96 --- /dev/null +++ b/tests/baselines/reference/typesOptionWildcard.types @@ -0,0 +1,33 @@ +//// [tests/cases/compiler/typesOptionWildcard.ts] //// + +=== /app.ts === +// With "types": ["*"], all @types packages are automatically included +// This is the opt-in to the old behavior +$.x; +>$.x : number +> : ^^^^^^ +>$ : { x: number; } +> : ^^^^^ ^^^ +>x : number +> : ^^^^^^ + +_.map; +>_.map : any +>_ : { map: any; } +> : ^^^^^^^ ^^^ +>map : any +> : ^^^ + +=== /node_modules/@types/jquery/index.d.ts === +declare var $: { x: number }; +>$ : { x: number; } +> : ^^^^^ ^^^ +>x : number +> : ^^^^^^ + +=== /node_modules/@types/lodash/index.d.ts === +declare var _: { map: any }; +>_ : { map: any; } +> : ^^^^^^^ ^^^ +>map : any + diff --git a/tests/cases/compiler/typesOptionDefaultEmpty.ts b/tests/cases/compiler/typesOptionDefaultEmpty.ts new file mode 100644 index 00000000000..9e49215bd9b --- /dev/null +++ b/tests/cases/compiler/typesOptionDefaultEmpty.ts @@ -0,0 +1,17 @@ +// @traceResolution: true +// @noImplicitReferences: true +// @currentDirectory: / + +// @filename: /tsconfig.json +{} + +// @filename: /node_modules/@types/jquery/index.d.ts +declare var $: { x: number }; + +// @filename: /node_modules/@types/lodash/index.d.ts +declare var _: { map: any }; + +// @filename: /app.ts +// With the new default behavior, @types packages are not automatically included +// unless explicitly listed in the types array or imported +const value = 42; diff --git a/tests/cases/compiler/typesOptionExplicitList.ts b/tests/cases/compiler/typesOptionExplicitList.ts new file mode 100644 index 00000000000..3a699bb8faf --- /dev/null +++ b/tests/cases/compiler/typesOptionExplicitList.ts @@ -0,0 +1,19 @@ +// @traceResolution: true +// @noImplicitReferences: true +// @currentDirectory: / + +// @filename: /tsconfig.json +{ "compilerOptions": { "types": ["jquery"] } } + +// @filename: /node_modules/@types/jquery/index.d.ts +declare var $: { x: number }; + +// @filename: /node_modules/@types/lodash/index.d.ts +declare var _: { map: any }; + +// @filename: /app.ts +// With "types": ["jquery"], only jquery is included +$.x; + +// lodash is not included, so this should error +_.map; diff --git a/tests/cases/compiler/typesOptionWildcard.ts b/tests/cases/compiler/typesOptionWildcard.ts new file mode 100644 index 00000000000..165b6e30f03 --- /dev/null +++ b/tests/cases/compiler/typesOptionWildcard.ts @@ -0,0 +1,18 @@ +// @traceResolution: true +// @noImplicitReferences: true +// @currentDirectory: / + +// @filename: /tsconfig.json +{ "compilerOptions": { "types": ["*"] } } + +// @filename: /node_modules/@types/jquery/index.d.ts +declare var $: { x: number }; + +// @filename: /node_modules/@types/lodash/index.d.ts +declare var _: { map: any }; + +// @filename: /app.ts +// With "types": ["*"], all @types packages are automatically included +// This is the opt-in to the old behavior +$.x; +_.map;