Skip to content

Commit 82a3fea

Browse files
committed
Treat function paramters in a .js file with no JSDoc as optional
1 parent 501084a commit 82a3fea

6 files changed

+117
-1
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5226,6 +5226,7 @@ namespace ts {
52265226
let hasThisParameter: boolean;
52275227
const iife = getImmediatelyInvokedFunctionExpression(declaration);
52285228
const isJSConstructSignature = isJSDocConstructSignature(declaration);
5229+
const isUntypedSignatureInJSFile = !iife && !isJSConstructSignature && isInJavaScriptFile(declaration);
52295230

52305231
// If this is a JSDoc construct signature, then skip the first parameter in the
52315232
// parameter list. The first parameter represents the return type of the construct
@@ -5251,10 +5252,13 @@ namespace ts {
52515252
hasLiteralTypes = true;
52525253
}
52535254

5255+
const isUntypedParamInJSFile = isUntypedSignatureInJSFile && !param.type && !getJSDocParameterTags(param);
5256+
52545257
// Record a new minimum argument count if this is not an optional parameter
52555258
const isOptionalParameter = param.initializer || param.questionToken || param.dotDotDotToken ||
52565259
iife && parameters.length > iife.arguments.length && !param.type ||
5257-
isJSDocOptionalParameter(param);
5260+
isJSDocOptionalParameter(param) ||
5261+
isUntypedParamInJSFile;
52585262
if (!isOptionalParameter) {
52595263
minArgumentCount = parameters.length;
52605264
}
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)