Skip to content

Commit f45add0

Browse files
brendankennyRyanCavanaugh
authored andcommitted
checkJs: require JSDoc type argument for Array, Object, and Promise in noImplicitAny (#32829)
* Require type argument for JSDoc Array, Object, and Promise in noImplicitAny * add jsdoc Array/Object/Promise noImplicitAny tests
1 parent 489abca commit f45add0

13 files changed

+476
-13
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9332,10 +9332,10 @@ namespace ts {
93329332
return globalFunctionType;
93339333
case "Array":
93349334
case "array":
9335-
return !typeArgs || !typeArgs.length ? anyArrayType : undefined;
9335+
return (!typeArgs || !typeArgs.length) && !noImplicitAny ? anyArrayType : undefined;
93369336
case "Promise":
93379337
case "promise":
9338-
return !typeArgs || !typeArgs.length ? createPromiseType(anyType) : undefined;
9338+
return (!typeArgs || !typeArgs.length) && !noImplicitAny ? createPromiseType(anyType) : undefined;
93399339
case "Object":
93409340
if (typeArgs && typeArgs.length === 2) {
93419341
if (isJSDocIndexSignature(node)) {
@@ -9347,7 +9347,7 @@ namespace ts {
93479347
return anyType;
93489348
}
93499349
checkNoTypeArguments(node);
9350-
return anyType;
9350+
return !noImplicitAny ? anyType : undefined;
93519351
}
93529352
}
93539353
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
=== tests/cases/compiler/jsdocArrayObjectPromiseImplicitAny.js ===
2+
/** @type {Array} */
3+
var anyArray = [5];
4+
>anyArray : Symbol(anyArray, Decl(jsdocArrayObjectPromiseImplicitAny.js, 1, 3))
5+
6+
/** @type {Array<number>} */
7+
var numberArray = [5];
8+
>numberArray : Symbol(numberArray, Decl(jsdocArrayObjectPromiseImplicitAny.js, 4, 3))
9+
10+
/**
11+
* @param {Array} arr
12+
* @return {Array}
13+
*/
14+
function returnAnyArray(arr) {
15+
>returnAnyArray : Symbol(returnAnyArray, Decl(jsdocArrayObjectPromiseImplicitAny.js, 4, 22))
16+
>arr : Symbol(arr, Decl(jsdocArrayObjectPromiseImplicitAny.js, 10, 24))
17+
18+
return arr;
19+
>arr : Symbol(arr, Decl(jsdocArrayObjectPromiseImplicitAny.js, 10, 24))
20+
}
21+
22+
/** @type {Promise} */
23+
var anyPromise = Promise.resolve(5);
24+
>anyPromise : Symbol(anyPromise, Decl(jsdocArrayObjectPromiseImplicitAny.js, 15, 3))
25+
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
26+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
27+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
28+
29+
/** @type {Promise<number>} */
30+
var numberPromise = Promise.resolve(5);
31+
>numberPromise : Symbol(numberPromise, Decl(jsdocArrayObjectPromiseImplicitAny.js, 18, 3))
32+
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
33+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
34+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
35+
36+
/**
37+
* @param {Promise} pr
38+
* @return {Promise}
39+
*/
40+
function returnAnyPromise(pr) {
41+
>returnAnyPromise : Symbol(returnAnyPromise, Decl(jsdocArrayObjectPromiseImplicitAny.js, 18, 39))
42+
>pr : Symbol(pr, Decl(jsdocArrayObjectPromiseImplicitAny.js, 24, 26))
43+
44+
return pr;
45+
>pr : Symbol(pr, Decl(jsdocArrayObjectPromiseImplicitAny.js, 24, 26))
46+
}
47+
48+
/** @type {Object} */
49+
var anyObject = {valueOf: 1}; // not an error since assigning to any.
50+
>anyObject : Symbol(anyObject, Decl(jsdocArrayObjectPromiseImplicitAny.js, 29, 3))
51+
>valueOf : Symbol(valueOf, Decl(jsdocArrayObjectPromiseImplicitAny.js, 29, 17))
52+
53+
/** @type {Object<string, number>} */
54+
var paramedObject = {valueOf: 1};
55+
>paramedObject : Symbol(paramedObject, Decl(jsdocArrayObjectPromiseImplicitAny.js, 32, 3))
56+
>valueOf : Symbol(valueOf, Decl(jsdocArrayObjectPromiseImplicitAny.js, 32, 21))
57+
58+
/**
59+
* @param {Object} obj
60+
* @return {Object}
61+
*/
62+
function returnAnyObject(obj) {
63+
>returnAnyObject : Symbol(returnAnyObject, Decl(jsdocArrayObjectPromiseImplicitAny.js, 32, 33))
64+
>obj : Symbol(obj, Decl(jsdocArrayObjectPromiseImplicitAny.js, 38, 25))
65+
66+
return obj;
67+
>obj : Symbol(obj, Decl(jsdocArrayObjectPromiseImplicitAny.js, 38, 25))
68+
}
69+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
=== tests/cases/compiler/jsdocArrayObjectPromiseImplicitAny.js ===
2+
/** @type {Array} */
3+
var anyArray = [5];
4+
>anyArray : any[]
5+
>[5] : number[]
6+
>5 : 5
7+
8+
/** @type {Array<number>} */
9+
var numberArray = [5];
10+
>numberArray : number[]
11+
>[5] : number[]
12+
>5 : 5
13+
14+
/**
15+
* @param {Array} arr
16+
* @return {Array}
17+
*/
18+
function returnAnyArray(arr) {
19+
>returnAnyArray : (arr: any[]) => any[]
20+
>arr : any[]
21+
22+
return arr;
23+
>arr : any[]
24+
}
25+
26+
/** @type {Promise} */
27+
var anyPromise = Promise.resolve(5);
28+
>anyPromise : Promise<any>
29+
>Promise.resolve(5) : Promise<number>
30+
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
31+
>Promise : PromiseConstructor
32+
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
33+
>5 : 5
34+
35+
/** @type {Promise<number>} */
36+
var numberPromise = Promise.resolve(5);
37+
>numberPromise : Promise<number>
38+
>Promise.resolve(5) : Promise<number>
39+
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
40+
>Promise : PromiseConstructor
41+
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
42+
>5 : 5
43+
44+
/**
45+
* @param {Promise} pr
46+
* @return {Promise}
47+
*/
48+
function returnAnyPromise(pr) {
49+
>returnAnyPromise : (pr: Promise<any>) => Promise<any>
50+
>pr : Promise<any>
51+
52+
return pr;
53+
>pr : Promise<any>
54+
}
55+
56+
/** @type {Object} */
57+
var anyObject = {valueOf: 1}; // not an error since assigning to any.
58+
>anyObject : any
59+
>{valueOf: 1} : { valueOf: number; }
60+
>valueOf : number
61+
>1 : 1
62+
63+
/** @type {Object<string, number>} */
64+
var paramedObject = {valueOf: 1};
65+
>paramedObject : { [x: string]: number; }
66+
>{valueOf: 1} : { valueOf: number; }
67+
>valueOf : number
68+
>1 : 1
69+
70+
/**
71+
* @param {Object} obj
72+
* @return {Object}
73+
*/
74+
function returnAnyObject(obj) {
75+
>returnAnyObject : (obj: any) => any
76+
>obj : any
77+
78+
return obj;
79+
>obj : any
80+
}
81+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
tests/cases/compiler/jsdocArrayObjectPromiseNoImplicitAny.js(1,12): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
2+
tests/cases/compiler/jsdocArrayObjectPromiseNoImplicitAny.js(8,12): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
3+
tests/cases/compiler/jsdocArrayObjectPromiseNoImplicitAny.js(9,13): error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
4+
tests/cases/compiler/jsdocArrayObjectPromiseNoImplicitAny.js(15,12): error TS2314: Generic type 'Promise<T>' requires 1 type argument(s).
5+
tests/cases/compiler/jsdocArrayObjectPromiseNoImplicitAny.js(22,12): error TS2314: Generic type 'Promise<T>' requires 1 type argument(s).
6+
tests/cases/compiler/jsdocArrayObjectPromiseNoImplicitAny.js(23,13): error TS2314: Generic type 'Promise<T>' requires 1 type argument(s).
7+
tests/cases/compiler/jsdocArrayObjectPromiseNoImplicitAny.js(30,21): error TS2322: Type 'number' is not assignable to type '() => Object'.
8+
9+
10+
==== tests/cases/compiler/jsdocArrayObjectPromiseNoImplicitAny.js (7 errors) ====
11+
/** @type {Array} */
12+
~~~~~
13+
!!! error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
14+
var notAnyArray = [5];
15+
16+
/** @type {Array<number>} */
17+
var numberArray = [5];
18+
19+
/**
20+
* @param {Array} arr
21+
~~~~~
22+
!!! error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
23+
* @return {Array}
24+
~~~~~
25+
!!! error TS2314: Generic type 'Array<T>' requires 1 type argument(s).
26+
*/
27+
function returnNotAnyArray(arr) {
28+
return arr;
29+
}
30+
31+
/** @type {Promise} */
32+
~~~~~~~
33+
!!! error TS2314: Generic type 'Promise<T>' requires 1 type argument(s).
34+
var notAnyPromise = Promise.resolve(5);
35+
36+
/** @type {Promise<number>} */
37+
var numberPromise = Promise.resolve(5);
38+
39+
/**
40+
* @param {Promise} pr
41+
~~~~~~~
42+
!!! error TS2314: Generic type 'Promise<T>' requires 1 type argument(s).
43+
* @return {Promise}
44+
~~~~~~~
45+
!!! error TS2314: Generic type 'Promise<T>' requires 1 type argument(s).
46+
*/
47+
function returnNotAnyPromise(pr) {
48+
return pr;
49+
}
50+
51+
/** @type {Object} */
52+
var notAnyObject = {valueOf: 1}; // error since assigning to Object, not any.
53+
~~~~~~~
54+
!!! error TS2322: Type 'number' is not assignable to type '() => Object'.
55+
56+
/** @type {Object<string, number>} */
57+
var paramedObject = {valueOf: 1};
58+
59+
/**
60+
* @param {Object} obj
61+
* @return {Object}
62+
*/
63+
function returnNotAnyObject(obj) {
64+
return obj;
65+
}
66+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
=== tests/cases/compiler/jsdocArrayObjectPromiseNoImplicitAny.js ===
2+
/** @type {Array} */
3+
var notAnyArray = [5];
4+
>notAnyArray : Symbol(notAnyArray, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 1, 3))
5+
6+
/** @type {Array<number>} */
7+
var numberArray = [5];
8+
>numberArray : Symbol(numberArray, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 4, 3))
9+
10+
/**
11+
* @param {Array} arr
12+
* @return {Array}
13+
*/
14+
function returnNotAnyArray(arr) {
15+
>returnNotAnyArray : Symbol(returnNotAnyArray, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 4, 22))
16+
>arr : Symbol(arr, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 10, 27))
17+
18+
return arr;
19+
>arr : Symbol(arr, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 10, 27))
20+
}
21+
22+
/** @type {Promise} */
23+
var notAnyPromise = Promise.resolve(5);
24+
>notAnyPromise : Symbol(notAnyPromise, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 15, 3))
25+
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
26+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
27+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
28+
29+
/** @type {Promise<number>} */
30+
var numberPromise = Promise.resolve(5);
31+
>numberPromise : Symbol(numberPromise, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 18, 3))
32+
>Promise.resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
33+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
34+
>resolve : Symbol(PromiseConstructor.resolve, Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --))
35+
36+
/**
37+
* @param {Promise} pr
38+
* @return {Promise}
39+
*/
40+
function returnNotAnyPromise(pr) {
41+
>returnNotAnyPromise : Symbol(returnNotAnyPromise, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 18, 39))
42+
>pr : Symbol(pr, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 24, 29))
43+
44+
return pr;
45+
>pr : Symbol(pr, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 24, 29))
46+
}
47+
48+
/** @type {Object} */
49+
var notAnyObject = {valueOf: 1}; // error since assigning to Object, not any.
50+
>notAnyObject : Symbol(notAnyObject, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 29, 3))
51+
>valueOf : Symbol(valueOf, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 29, 20))
52+
53+
/** @type {Object<string, number>} */
54+
var paramedObject = {valueOf: 1};
55+
>paramedObject : Symbol(paramedObject, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 32, 3))
56+
>valueOf : Symbol(valueOf, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 32, 21))
57+
58+
/**
59+
* @param {Object} obj
60+
* @return {Object}
61+
*/
62+
function returnNotAnyObject(obj) {
63+
>returnNotAnyObject : Symbol(returnNotAnyObject, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 32, 33))
64+
>obj : Symbol(obj, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 38, 28))
65+
66+
return obj;
67+
>obj : Symbol(obj, Decl(jsdocArrayObjectPromiseNoImplicitAny.js, 38, 28))
68+
}
69+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
=== tests/cases/compiler/jsdocArrayObjectPromiseNoImplicitAny.js ===
2+
/** @type {Array} */
3+
var notAnyArray = [5];
4+
>notAnyArray : any[]
5+
>[5] : number[]
6+
>5 : 5
7+
8+
/** @type {Array<number>} */
9+
var numberArray = [5];
10+
>numberArray : number[]
11+
>[5] : number[]
12+
>5 : 5
13+
14+
/**
15+
* @param {Array} arr
16+
* @return {Array}
17+
*/
18+
function returnNotAnyArray(arr) {
19+
>returnNotAnyArray : (arr: any[]) => any[]
20+
>arr : any[]
21+
22+
return arr;
23+
>arr : any[]
24+
}
25+
26+
/** @type {Promise} */
27+
var notAnyPromise = Promise.resolve(5);
28+
>notAnyPromise : Promise<any>
29+
>Promise.resolve(5) : Promise<number>
30+
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
31+
>Promise : PromiseConstructor
32+
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
33+
>5 : 5
34+
35+
/** @type {Promise<number>} */
36+
var numberPromise = Promise.resolve(5);
37+
>numberPromise : Promise<number>
38+
>Promise.resolve(5) : Promise<number>
39+
>Promise.resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
40+
>Promise : PromiseConstructor
41+
>resolve : { <T>(value: T | PromiseLike<T>): Promise<T>; (): Promise<void>; }
42+
>5 : 5
43+
44+
/**
45+
* @param {Promise} pr
46+
* @return {Promise}
47+
*/
48+
function returnNotAnyPromise(pr) {
49+
>returnNotAnyPromise : (pr: Promise<any>) => Promise<any>
50+
>pr : Promise<any>
51+
52+
return pr;
53+
>pr : Promise<any>
54+
}
55+
56+
/** @type {Object} */
57+
var notAnyObject = {valueOf: 1}; // error since assigning to Object, not any.
58+
>notAnyObject : Object
59+
>{valueOf: 1} : { valueOf: number; }
60+
>valueOf : number
61+
>1 : 1
62+
63+
/** @type {Object<string, number>} */
64+
var paramedObject = {valueOf: 1};
65+
>paramedObject : { [x: string]: number; }
66+
>{valueOf: 1} : { valueOf: number; }
67+
>valueOf : number
68+
>1 : 1
69+
70+
/**
71+
* @param {Object} obj
72+
* @return {Object}
73+
*/
74+
function returnNotAnyObject(obj) {
75+
>returnNotAnyObject : (obj: Object) => Object
76+
>obj : Object
77+
78+
return obj;
79+
>obj : Object
80+
}
81+

0 commit comments

Comments
 (0)