Skip to content

Commit 7db2335

Browse files
committed
Add tests for all @overload tags in one comment
1 parent f659657 commit 7db2335

8 files changed

+650
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
//// [jsFileFunctionOverloads2.js]
2+
// Also works if all @overload tags are combined in one comment.
3+
/**
4+
* @overload
5+
* @param {number} x
6+
* @returns {'number'}
7+
*
8+
* @overload
9+
* @param {string} x
10+
* @returns {'string'}
11+
*
12+
* @overload
13+
* @param {boolean} x
14+
* @returns {'boolean'}
15+
*
16+
* @param {unknown} x
17+
* @returns {string}
18+
*/
19+
function getTypeName(x) {
20+
return typeof x;
21+
}
22+
23+
/**
24+
* @template T
25+
* @param {T} x
26+
* @returns {T}
27+
*/
28+
const identity = x => x;
29+
30+
/**
31+
* @template T
32+
* @template U
33+
* @overload
34+
* @param {T[]} array
35+
* @param {(x: T) => U[]} iterable
36+
* @returns {U[]}
37+
*
38+
* @overload
39+
* @param {T[][]} array
40+
* @returns {T[]}
41+
*
42+
* @param {unknown[]} array
43+
* @param {(x: unknown) => unknown} iterable
44+
* @returns {unknown[]}
45+
*/
46+
function flatMap(array, iterable = identity) {
47+
/** @type {unknown[]} */
48+
const result = [];
49+
for (let i = 0; i < array.length; i += 1) {
50+
result.push(.../** @type {unknown[]} */(iterable(array[i])));
51+
}
52+
return result;
53+
}
54+
55+
56+
//// [jsFileFunctionOverloads2.js]
57+
// Also works if all @overload tags are combined in one comment.
58+
/**
59+
* @overload
60+
* @param {number} x
61+
* @returns {'number'}
62+
*
63+
* @overload
64+
* @param {string} x
65+
* @returns {'string'}
66+
*
67+
* @overload
68+
* @param {boolean} x
69+
* @returns {'boolean'}
70+
*
71+
* @param {unknown} x
72+
* @returns {string}
73+
*/
74+
function getTypeName(x) {
75+
return typeof x;
76+
}
77+
/**
78+
* @template T
79+
* @param {T} x
80+
* @returns {T}
81+
*/
82+
var identity = function (x) { return x; };
83+
/**
84+
* @template T
85+
* @template U
86+
* @overload
87+
* @param {T[]} array
88+
* @param {(x: T) => U[]} iterable
89+
* @returns {U[]}
90+
*
91+
* @overload
92+
* @param {T[][]} array
93+
* @returns {T[]}
94+
*
95+
* @param {unknown[]} array
96+
* @param {(x: unknown) => unknown} iterable
97+
* @returns {unknown[]}
98+
*/
99+
function flatMap(array, iterable) {
100+
if (iterable === void 0) { iterable = identity; }
101+
/** @type {unknown[]} */
102+
var result = [];
103+
for (var i = 0; i < array.length; i += 1) {
104+
result.push.apply(result, /** @type {unknown[]} */ (iterable(array[i])));
105+
}
106+
return result;
107+
}
108+
109+
110+
//// [jsFileFunctionOverloads2.d.ts]
111+
declare function getTypeName(x: number): 'number';
112+
declare function getTypeName(x: string): 'string';
113+
declare function getTypeName(x: boolean): 'boolean';
114+
declare function flatMap<T, U>(array: T[], iterable: (x: T) => U[]): U[];
115+
declare function flatMap<T, U>(array: T[][]): T[];
116+
/**
117+
* @template T
118+
* @param {T} x
119+
* @returns {T}
120+
*/
121+
declare function identity<T>(x: T): T;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
=== tests/cases/compiler/jsFileFunctionOverloads2.js ===
2+
// Also works if all @overload tags are combined in one comment.
3+
/**
4+
* @overload
5+
* @param {number} x
6+
* @returns {'number'}
7+
*
8+
* @overload
9+
* @param {string} x
10+
* @returns {'string'}
11+
*
12+
* @overload
13+
* @param {boolean} x
14+
* @returns {'boolean'}
15+
*
16+
* @param {unknown} x
17+
* @returns {string}
18+
*/
19+
function getTypeName(x) {
20+
>getTypeName : Symbol(getTypeName, Decl(jsFileFunctionOverloads2.js, 0, 0))
21+
>x : Symbol(x, Decl(jsFileFunctionOverloads2.js, 17, 22))
22+
23+
return typeof x;
24+
>x : Symbol(x, Decl(jsFileFunctionOverloads2.js, 17, 22))
25+
}
26+
27+
/**
28+
* @template T
29+
* @param {T} x
30+
* @returns {T}
31+
*/
32+
const identity = x => x;
33+
>identity : Symbol(identity, Decl(jsFileFunctionOverloads2.js, 26, 5))
34+
>x : Symbol(x, Decl(jsFileFunctionOverloads2.js, 26, 16))
35+
>x : Symbol(x, Decl(jsFileFunctionOverloads2.js, 26, 16))
36+
37+
/**
38+
* @template T
39+
* @template U
40+
* @overload
41+
* @param {T[]} array
42+
* @param {(x: T) => U[]} iterable
43+
* @returns {U[]}
44+
*
45+
* @overload
46+
* @param {T[][]} array
47+
* @returns {T[]}
48+
*
49+
* @param {unknown[]} array
50+
* @param {(x: unknown) => unknown} iterable
51+
* @returns {unknown[]}
52+
*/
53+
function flatMap(array, iterable = identity) {
54+
>flatMap : Symbol(flatMap, Decl(jsFileFunctionOverloads2.js, 26, 24))
55+
>array : Symbol(array, Decl(jsFileFunctionOverloads2.js, 44, 17))
56+
>iterable : Symbol(iterable, Decl(jsFileFunctionOverloads2.js, 44, 23))
57+
>identity : Symbol(identity, Decl(jsFileFunctionOverloads2.js, 26, 5))
58+
59+
/** @type {unknown[]} */
60+
const result = [];
61+
>result : Symbol(result, Decl(jsFileFunctionOverloads2.js, 46, 7))
62+
63+
for (let i = 0; i < array.length; i += 1) {
64+
>i : Symbol(i, Decl(jsFileFunctionOverloads2.js, 47, 10))
65+
>i : Symbol(i, Decl(jsFileFunctionOverloads2.js, 47, 10))
66+
>array.length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
67+
>array : Symbol(array, Decl(jsFileFunctionOverloads2.js, 44, 17))
68+
>length : Symbol(Array.length, Decl(lib.es5.d.ts, --, --))
69+
>i : Symbol(i, Decl(jsFileFunctionOverloads2.js, 47, 10))
70+
71+
result.push(.../** @type {unknown[]} */(iterable(array[i])));
72+
>result.push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
73+
>result : Symbol(result, Decl(jsFileFunctionOverloads2.js, 46, 7))
74+
>push : Symbol(Array.push, Decl(lib.es5.d.ts, --, --))
75+
>iterable : Symbol(iterable, Decl(jsFileFunctionOverloads2.js, 44, 23))
76+
>array : Symbol(array, Decl(jsFileFunctionOverloads2.js, 44, 17))
77+
>i : Symbol(i, Decl(jsFileFunctionOverloads2.js, 47, 10))
78+
}
79+
return result;
80+
>result : Symbol(result, Decl(jsFileFunctionOverloads2.js, 46, 7))
81+
}
82+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
=== tests/cases/compiler/jsFileFunctionOverloads2.js ===
2+
// Also works if all @overload tags are combined in one comment.
3+
/**
4+
* @overload
5+
* @param {number} x
6+
* @returns {'number'}
7+
*
8+
* @overload
9+
* @param {string} x
10+
* @returns {'string'}
11+
*
12+
* @overload
13+
* @param {boolean} x
14+
* @returns {'boolean'}
15+
*
16+
* @param {unknown} x
17+
* @returns {string}
18+
*/
19+
function getTypeName(x) {
20+
>getTypeName : { (x: number): 'number'; (x: string): 'string'; (x: boolean): 'boolean'; }
21+
>x : unknown
22+
23+
return typeof x;
24+
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
25+
>x : unknown
26+
}
27+
28+
/**
29+
* @template T
30+
* @param {T} x
31+
* @returns {T}
32+
*/
33+
const identity = x => x;
34+
>identity : <T>(x: T) => T
35+
>x => x : <T>(x: T) => T
36+
>x : T
37+
>x : T
38+
39+
/**
40+
* @template T
41+
* @template U
42+
* @overload
43+
* @param {T[]} array
44+
* @param {(x: T) => U[]} iterable
45+
* @returns {U[]}
46+
*
47+
* @overload
48+
* @param {T[][]} array
49+
* @returns {T[]}
50+
*
51+
* @param {unknown[]} array
52+
* @param {(x: unknown) => unknown} iterable
53+
* @returns {unknown[]}
54+
*/
55+
function flatMap(array, iterable = identity) {
56+
>flatMap : { <T, U>(array: T[], iterable: (x: T) => U[]): U[]; <T, U>(array: T[][]): T[]; }
57+
>array : unknown[]
58+
>iterable : (x: unknown) => unknown
59+
>identity : <T>(x: T) => T
60+
61+
/** @type {unknown[]} */
62+
const result = [];
63+
>result : unknown[]
64+
>[] : undefined[]
65+
66+
for (let i = 0; i < array.length; i += 1) {
67+
>i : number
68+
>0 : 0
69+
>i < array.length : boolean
70+
>i : number
71+
>array.length : number
72+
>array : unknown[]
73+
>length : number
74+
>i += 1 : number
75+
>i : number
76+
>1 : 1
77+
78+
result.push(.../** @type {unknown[]} */(iterable(array[i])));
79+
>result.push(.../** @type {unknown[]} */(iterable(array[i]))) : number
80+
>result.push : (...items: unknown[]) => number
81+
>result : unknown[]
82+
>push : (...items: unknown[]) => number
83+
>.../** @type {unknown[]} */(iterable(array[i])) : unknown
84+
>(iterable(array[i])) : unknown[]
85+
>iterable(array[i]) : unknown
86+
>iterable : (x: unknown) => unknown
87+
>array[i] : unknown
88+
>array : unknown[]
89+
>i : number
90+
}
91+
return result;
92+
>result : unknown[]
93+
}
94+

0 commit comments

Comments
 (0)