Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10499,7 +10499,7 @@ namespace ts {
const signature = getSignatureFromDeclaration(node.parent);
const parameterIndex = node.parent.parameters.indexOf(node);
Debug.assert(parameterIndex >= 0);
return parameterIndex >= getMinArgumentCount(signature);
return parameterIndex >= getMinArgumentCount(signature, /*strongArityForUntypedJS*/ true);
}
const iife = getImmediatelyInvokedFunctionExpression(node.parent);
if (iife) {
Expand Down Expand Up @@ -10590,6 +10590,9 @@ namespace ts {
isValueSignatureDeclaration(declaration) &&
!hasJSDocParameterTags(declaration) &&
!getJSDocType(declaration);
if (isUntypedSignatureInJSFile) {
flags |= SignatureFlags.IsUntypedSignatureInJSFile;
}

// If this is a JSDoc construct signature, then skip the first parameter in the
// parameter list. The first parameter represents the return type of the construct
Expand Down Expand Up @@ -10620,7 +10623,6 @@ namespace ts {
const isOptionalParameter = isOptionalJSDocParameterTag(param) ||
param.initializer || param.questionToken || param.dotDotDotToken ||
iife && parameters.length > iife.arguments.length && !type ||
isUntypedSignatureInJSFile ||
isJSDocOptionalParameter(param);
if (!isOptionalParameter) {
minArgumentCount = parameters.length;
Expand Down Expand Up @@ -26340,7 +26342,7 @@ namespace ts {
return length;
}

function getMinArgumentCount(signature: Signature) {
function getMinArgumentCount(signature: Signature, strongArityForUntypedJS?: boolean) {
if (signatureHasRestParameter(signature)) {
const restType = getTypeOfSymbol(signature.parameters[signature.parameters.length - 1]);
if (isTupleType(restType)) {
Expand All @@ -26350,6 +26352,9 @@ namespace ts {
}
}
}
if (!strongArityForUntypedJS && signature.flags & SignatureFlags.IsUntypedSignatureInJSFile) {
return 0;
}
return signature.minArgumentCount;
}

Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4817,6 +4817,7 @@ namespace ts {
HasLiteralTypes = 1 << 1, // Indicates signature is specialized
IsInnerCallChain = 1 << 2, // Indicates signature comes from a CallChain nested in an outer OptionalChain
IsOuterCallChain = 1 << 3, // Indicates signature comes from a CallChain that is the outermost chain of an optional expression
IsUntypedSignatureInJSFile = 1 << 4, // Indicates signature is from a js file and has no types

// We do not propagate `IsInnerCallChain` to instantiated signatures, as that would result in us
// attempting to add `| undefined` on each recursive call to `getReturnTypeOfSignature` when
Expand Down
27 changes: 27 additions & 0 deletions tests/baselines/reference/jsDeclarationsClassLeadingOptional.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//// [bar.js]
export class Z {
f(x = 1, y) {
return [x, y];
}
}

//// [bar.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Z = void 0;
var Z = /** @class */ (function () {
function Z() {
}
Z.prototype.f = function (x, y) {
if (x === void 0) { x = 1; }
return [x, y];
};
return Z;
}());
exports.Z = Z;


//// [bar.d.ts]
export class Z {
f(x: number, y: any): any[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/conformance/jsdoc/declarations/bar.js ===
export class Z {
>Z : Symbol(Z, Decl(bar.js, 0, 0))

f(x = 1, y) {
>f : Symbol(Z.f, Decl(bar.js, 0, 16))
>x : Symbol(x, Decl(bar.js, 1, 6))
>y : Symbol(y, Decl(bar.js, 1, 12))

return [x, y];
>x : Symbol(x, Decl(bar.js, 1, 6))
>y : Symbol(y, Decl(bar.js, 1, 12))
}
}
16 changes: 16 additions & 0 deletions tests/baselines/reference/jsDeclarationsClassLeadingOptional.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=== tests/cases/conformance/jsdoc/declarations/bar.js ===
export class Z {
>Z : Z

f(x = 1, y) {
>f : (x: number, y: any) => any[]
>x : number
>1 : 1
>y : any

return [x, y];
>[x, y] : any[]
>x : number
>y : any
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @allowJs: true
// @checkJs: true
// @target: es5
// @outDir: ./out
// @declaration: true
// @filename: bar.js
export class Z {
f(x = 1, y) {
return [x, y];
}
}