mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
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>
This commit is contained in:
parent
4a9fccf76d
commit
2b70067c56
@ -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
|
||||
|
||||
18
tests/baselines/reference/typesOptionDefaultEmpty.js
Normal file
18
tests/baselines/reference/typesOptionDefaultEmpty.js
Normal file
@ -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;
|
||||
@ -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))
|
||||
|
||||
@ -0,0 +1 @@
|
||||
[]
|
||||
11
tests/baselines/reference/typesOptionDefaultEmpty.types
Normal file
11
tests/baselines/reference/typesOptionDefaultEmpty.types
Normal file
@ -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
|
||||
> : ^^
|
||||
|
||||
21
tests/baselines/reference/typesOptionExplicitList.errors.txt
Normal file
21
tests/baselines/reference/typesOptionExplicitList.errors.txt
Normal file
@ -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 };
|
||||
|
||||
21
tests/baselines/reference/typesOptionExplicitList.js
Normal file
21
tests/baselines/reference/typesOptionExplicitList.js
Normal file
@ -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;
|
||||
17
tests/baselines/reference/typesOptionExplicitList.symbols
Normal file
17
tests/baselines/reference/typesOptionExplicitList.symbols
Normal file
@ -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))
|
||||
|
||||
12
tests/baselines/reference/typesOptionExplicitList.trace.json
Normal file
12
tests/baselines/reference/typesOptionExplicitList.trace.json
Normal file
@ -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."
|
||||
]
|
||||
28
tests/baselines/reference/typesOptionExplicitList.types
Normal file
28
tests/baselines/reference/typesOptionExplicitList.types
Normal file
@ -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
|
||||
> : ^^^^^^
|
||||
|
||||
20
tests/baselines/reference/typesOptionWildcard.js
Normal file
20
tests/baselines/reference/typesOptionWildcard.js
Normal file
@ -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;
|
||||
25
tests/baselines/reference/typesOptionWildcard.symbols
Normal file
25
tests/baselines/reference/typesOptionWildcard.symbols
Normal file
@ -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))
|
||||
|
||||
22
tests/baselines/reference/typesOptionWildcard.trace.json
Normal file
22
tests/baselines/reference/typesOptionWildcard.trace.json
Normal file
@ -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."
|
||||
]
|
||||
33
tests/baselines/reference/typesOptionWildcard.types
Normal file
33
tests/baselines/reference/typesOptionWildcard.types
Normal file
@ -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
|
||||
|
||||
17
tests/cases/compiler/typesOptionDefaultEmpty.ts
Normal file
17
tests/cases/compiler/typesOptionDefaultEmpty.ts
Normal file
@ -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;
|
||||
19
tests/cases/compiler/typesOptionExplicitList.ts
Normal file
19
tests/cases/compiler/typesOptionExplicitList.ts
Normal file
@ -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;
|
||||
18
tests/cases/compiler/typesOptionWildcard.ts
Normal file
18
tests/cases/compiler/typesOptionWildcard.ts
Normal file
@ -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;
|
||||
Loading…
x
Reference in New Issue
Block a user