Skip to content

Commit 2fc634f

Browse files
authored
Merge pull request microsoft#13905 from Microsoft/optionalParametersInJSFunctions
Treat function paramters in a .js file with no JSDoc as optional
2 parents 4ec6848 + e76607e commit 2fc634f

7 files changed

+120
-1
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5241,6 +5241,7 @@ namespace ts {
52415241
let hasThisParameter: boolean;
52425242
const iife = getImmediatelyInvokedFunctionExpression(declaration);
52435243
const isJSConstructSignature = isJSDocConstructSignature(declaration);
5244+
const isUntypedSignatureInJSFile = !iife && !isJSConstructSignature && isInJavaScriptFile(declaration) && !hasJSDocParameterTags(declaration);
52445245

52455246
// If this is a JSDoc construct signature, then skip the first parameter in the
52465247
// parameter list. The first parameter represents the return type of the construct
@@ -5269,7 +5270,8 @@ namespace ts {
52695270
// Record a new minimum argument count if this is not an optional parameter
52705271
const isOptionalParameter = param.initializer || param.questionToken || param.dotDotDotToken ||
52715272
iife && parameters.length > iife.arguments.length && !param.type ||
5272-
isJSDocOptionalParameter(param);
5273+
isJSDocOptionalParameter(param) ||
5274+
isUntypedSignatureInJSFile;
52735275
if (!isOptionalParameter) {
52745276
minArgumentCount = parameters.length;
52755277
}

src/compiler/utilities.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,11 @@ namespace ts {
15181518
return map(getJSDocs(node), doc => doc.comment);
15191519
}
15201520

1521+
export function hasJSDocParameterTags(node: FunctionLikeDeclaration | SignatureDeclaration) {
1522+
const parameterTags = getJSDocTags(node, SyntaxKind.JSDocParameterTag);
1523+
return parameterTags && parameterTags.length > 0;
1524+
}
1525+
15211526
function getJSDocTags(node: Node, kind: SyntaxKind): JSDocTag[] {
15221527
const docs = getJSDocs(node);
15231528
if (docs) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/foo.js ===
2+
3+
function f(a, b, c) { }
4+
>f : Symbol(f, Decl(foo.js, 0, 0))
5+
>a : Symbol(a, Decl(foo.js, 1, 11))
6+
>b : Symbol(b, Decl(foo.js, 1, 13))
7+
>c : Symbol(c, Decl(foo.js, 1, 16))
8+
9+
10+
=== tests/cases/compiler/bar.ts ===
11+
f();
12+
>f : Symbol(f, Decl(foo.js, 0, 0))
13+
14+
f(1);
15+
>f : Symbol(f, Decl(foo.js, 0, 0))
16+
17+
f(1, 2);
18+
>f : Symbol(f, Decl(foo.js, 0, 0))
19+
20+
f(1, 2, 3);
21+
>f : Symbol(f, Decl(foo.js, 0, 0))
22+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/compiler/foo.js ===
2+
3+
function f(a, b, c) { }
4+
>f : (a: any, b: any, c: any) => void
5+
>a : any
6+
>b : any
7+
>c : any
8+
9+
10+
=== tests/cases/compiler/bar.ts ===
11+
f();
12+
>f() : void
13+
>f : (a: any, b: any, c: any) => void
14+
15+
f(1);
16+
>f(1) : void
17+
>f : (a: any, b: any, c: any) => void
18+
>1 : 1
19+
20+
f(1, 2);
21+
>f(1, 2) : void
22+
>f : (a: any, b: any, c: any) => void
23+
>1 : 1
24+
>2 : 2
25+
26+
f(1, 2, 3);
27+
>f(1, 2, 3) : void
28+
>f : (a: any, b: any, c: any) => void
29+
>1 : 1
30+
>2 : 2
31+
>3 : 3
32+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
tests/cases/compiler/bar.ts(1,1): error TS2346: Supplied parameters do not match any signature of call target.
2+
tests/cases/compiler/bar.ts(2,1): error TS2346: Supplied parameters do not match any signature of call target.
3+
tests/cases/compiler/bar.ts(3,1): error TS2346: Supplied parameters do not match any signature of call target.
4+
5+
6+
==== tests/cases/compiler/foo.js (0 errors) ====
7+
8+
/**
9+
* @param a
10+
* @param b
11+
* @param c
12+
*/
13+
function f(a, b, c) { }
14+
15+
16+
==== tests/cases/compiler/bar.ts (3 errors) ====
17+
f(); // Error
18+
~~~
19+
!!! error TS2346: Supplied parameters do not match any signature of call target.
20+
f(1); // Error
21+
~~~~
22+
!!! error TS2346: Supplied parameters do not match any signature of call target.
23+
f(1, 2); // Error
24+
~~~~~~~
25+
!!! error TS2346: Supplied parameters do not match any signature of call target.
26+
27+
f(1, 2, 3); // OK
28+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @allowJs: true
2+
// @noEmit: true
3+
4+
// @filename: foo.js
5+
function f(a, b, c) { }
6+
7+
8+
// @filename: bar.ts
9+
f();
10+
f(1);
11+
f(1, 2);
12+
f(1, 2, 3);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// @allowJs: true
2+
// @noEmit: true
3+
4+
// @filename: foo.js
5+
/**
6+
* @param a
7+
* @param b
8+
* @param c
9+
*/
10+
function f(a, b, c) { }
11+
12+
13+
// @filename: bar.ts
14+
f(); // Error
15+
f(1); // Error
16+
f(1, 2); // Error
17+
18+
f(1, 2, 3); // OK

0 commit comments

Comments
 (0)