@@ -6691,7 +6691,11 @@ namespace ts {
6691
6691
let hasThisParameter: boolean;
6692
6692
const iife = getImmediatelyInvokedFunctionExpression(declaration);
6693
6693
const isJSConstructSignature = isJSDocConstructSignature(declaration);
6694
- const isUntypedSignatureInJSFile = !iife && !isJSConstructSignature && isInJavaScriptFile(declaration) && !hasJSDocParameterTags(declaration);
6694
+ const isUntypedSignatureInJSFile = !iife &&
6695
+ isInJavaScriptFile(declaration) &&
6696
+ isValueSignatureDeclaration(declaration) &&
6697
+ !hasJSDocParameterTags(declaration) &&
6698
+ !getJSDocType(declaration);
6695
6699
6696
6700
// If this is a JSDoc construct signature, then skip the first parameter in the
6697
6701
// parameter list. The first parameter represents the return type of the construct
@@ -9100,7 +9104,8 @@ namespace ts {
9100
9104
}
9101
9105
9102
9106
function isContextSensitiveFunctionOrObjectLiteralMethod(func: Node): func is FunctionExpression | ArrowFunction | MethodDeclaration {
9103
- return (isFunctionExpressionOrArrowFunction(func) || isObjectLiteralMethod(func)) && isContextSensitiveFunctionLikeDeclaration(func);
9107
+ return (isInJavaScriptFile(func) && isFunctionDeclaration(func) || isFunctionExpressionOrArrowFunction(func) || isObjectLiteralMethod(func)) &&
9108
+ isContextSensitiveFunctionLikeDeclaration(func);
9104
9109
}
9105
9110
9106
9111
function getTypeWithoutSignatures(type: Type): Type {
@@ -14168,49 +14173,49 @@ namespace ts {
14168
14173
// Return contextual type of parameter or undefined if no contextual type is available
14169
14174
function getContextuallyTypedParameterType(parameter: ParameterDeclaration): Type | undefined {
14170
14175
const func = parameter.parent;
14171
- if (isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
14172
- const iife = getImmediatelyInvokedFunctionExpression(func);
14173
- if (iife && iife.arguments) {
14174
- const indexOfParameter = func.parameters.indexOf(parameter);
14175
- if (parameter.dotDotDotToken) {
14176
- const restTypes: Type[] = [];
14177
- for (let i = indexOfParameter; i < iife.arguments.length; i++) {
14178
- restTypes.push(getWidenedLiteralType(checkExpression(iife.arguments[i])));
14179
- }
14180
- return restTypes.length ? createArrayType(getUnionType(restTypes)) : undefined;
14181
- }
14182
- const links = getNodeLinks(iife);
14183
- const cached = links.resolvedSignature;
14184
- links.resolvedSignature = anySignature;
14185
- const type = indexOfParameter < iife.arguments.length ?
14186
- getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter])) :
14187
- parameter.initializer ? undefined : undefinedWideningType;
14188
- links.resolvedSignature = cached;
14189
- return type;
14176
+ if (!isContextSensitiveFunctionOrObjectLiteralMethod(func)) {
14177
+ return undefined;
14178
+ }
14179
+ const iife = getImmediatelyInvokedFunctionExpression(func);
14180
+ if (iife && iife.arguments) {
14181
+ const indexOfParameter = func.parameters.indexOf(parameter);
14182
+ if (parameter.dotDotDotToken) {
14183
+ const restTypes: Type[] = [];
14184
+ for (let i = indexOfParameter; i < iife.arguments.length; i++) {
14185
+ restTypes.push(getWidenedLiteralType(checkExpression(iife.arguments[i])));
14186
+ }
14187
+ return restTypes.length ? createArrayType(getUnionType(restTypes)) : undefined;
14188
+ }
14189
+ const links = getNodeLinks(iife);
14190
+ const cached = links.resolvedSignature;
14191
+ links.resolvedSignature = anySignature;
14192
+ const type = indexOfParameter < iife.arguments.length ?
14193
+ getWidenedLiteralType(checkExpression(iife.arguments[indexOfParameter])) :
14194
+ parameter.initializer ? undefined : undefinedWideningType;
14195
+ links.resolvedSignature = cached;
14196
+ return type;
14197
+ }
14198
+ const contextualSignature = getContextualSignature(func);
14199
+ if (contextualSignature) {
14200
+ const funcHasRestParameters = hasRestParameter(func);
14201
+ const len = func.parameters.length - (funcHasRestParameters ? 1 : 0);
14202
+ let indexOfParameter = func.parameters.indexOf(parameter);
14203
+ if (getThisParameter(func) !== undefined && !contextualSignature.thisParameter) {
14204
+ Debug.assert(indexOfParameter !== 0); // Otherwise we should not have called `getContextuallyTypedParameterType`.
14205
+ indexOfParameter -= 1;
14190
14206
}
14191
- const contextualSignature = getContextualSignature(func);
14192
- if (contextualSignature) {
14193
- const funcHasRestParameters = hasRestParameter(func);
14194
- const len = func.parameters.length - (funcHasRestParameters ? 1 : 0);
14195
- let indexOfParameter = func.parameters.indexOf(parameter);
14196
- if (getThisParameter(func) !== undefined && !contextualSignature.thisParameter) {
14197
- Debug.assert(indexOfParameter !== 0); // Otherwise we should not have called `getContextuallyTypedParameterType`.
14198
- indexOfParameter -= 1;
14199
- }
14200
14207
14201
- if (indexOfParameter < len) {
14202
- return getTypeAtPosition(contextualSignature, indexOfParameter);
14203
- }
14208
+ if (indexOfParameter < len) {
14209
+ return getTypeAtPosition(contextualSignature, indexOfParameter);
14210
+ }
14204
14211
14205
- // If last parameter is contextually rest parameter get its type
14206
- if (funcHasRestParameters &&
14207
- indexOfParameter === (func.parameters.length - 1) &&
14208
- isRestParameterIndex(contextualSignature, func.parameters.length - 1)) {
14209
- return getTypeOfSymbol(lastOrUndefined(contextualSignature.parameters));
14210
- }
14212
+ // If last parameter is contextually rest parameter get its type
14213
+ if (funcHasRestParameters &&
14214
+ indexOfParameter === (func.parameters.length - 1) &&
14215
+ isRestParameterIndex(contextualSignature, func.parameters.length - 1)) {
14216
+ return getTypeOfSymbol(lastOrUndefined(contextualSignature.parameters));
14211
14217
}
14212
14218
}
14213
- return undefined;
14214
14219
}
14215
14220
14216
14221
// In a variable, parameter or property declaration with a type annotation,
@@ -14773,7 +14778,16 @@ namespace ts {
14773
14778
// union type of return types from these signatures
14774
14779
function getContextualSignature(node: FunctionExpression | ArrowFunction | MethodDeclaration): Signature {
14775
14780
Debug.assert(node.kind !== SyntaxKind.MethodDeclaration || isObjectLiteralMethod(node));
14776
- const type = getContextualTypeForFunctionLikeDeclaration(node);
14781
+ let type: Type;
14782
+ if (isInJavaScriptFile(node)) {
14783
+ const jsdoc = getJSDocType(node);
14784
+ if (jsdoc) {
14785
+ type = getTypeFromTypeNode(jsdoc);
14786
+ }
14787
+ }
14788
+ if (!type) {
14789
+ type = getContextualTypeForFunctionLikeDeclaration(node);
14790
+ }
14777
14791
if (!type) {
14778
14792
return undefined;
14779
14793
}
0 commit comments