JSDoc @type tag optional parameters (#48132)

* JSDoc @type tag optional parameters

* Don't repeat isInJSFile() condition

* Exclude variable initializers

* Add tests for class methods

* Don't contextually type JS function declarations

* Update Baselines and/or Applied Lint Fixes

* Reword comment

Co-authored-by: TypeScript Bot <typescriptbot@microsoft.com>
This commit is contained in:
Jack Bates
2022-08-09 17:03:30 -07:00
committed by GitHub
parent 2513a2d98c
commit 35c6fbfee0
12 changed files with 208 additions and 27 deletions

View File

@@ -9065,10 +9065,8 @@ namespace ts {
return getReturnTypeOfSignature(getterSignature);
}
}
if (isInJSFile(declaration)) {
const type = getParameterTypeOfTypeTag(func, declaration);
if (type) return type;
}
const parameterTypeOfTypeTag = getParameterTypeOfTypeTag(func, declaration);
if (parameterTypeOfTypeTag) return parameterTypeOfTypeTag;
// Use contextual parameter type if one is available
const type = declaration.symbol.escapedName === InternalSymbolName.This ? getContextualThisParameterType(func) : getContextuallyTypedParameterType(declaration);
if (type) {
@@ -13117,7 +13115,14 @@ namespace ts {
continue;
}
}
result.push(getSignatureFromDeclaration(decl));
// If this is a function or method declaration, get the signature from the @type tag for the sake of optional parameters.
// Exclude contextually-typed kinds because we already apply the @type tag to the context, plus applying it here to the initializer would supress checks that the two are compatible.
result.push(
(!isFunctionExpressionOrArrowFunction(decl) &&
!isObjectLiteralMethod(decl) &&
getSignatureOfTypeTag(decl)) ||
getSignatureFromDeclaration(decl)
);
}
return result;
}
@@ -13152,7 +13157,7 @@ namespace ts {
else {
const type = signature.declaration && getEffectiveReturnTypeNode(signature.declaration);
let jsdocPredicate: TypePredicate | undefined;
if (!type && isInJSFile(signature.declaration)) {
if (!type) {
const jsdocSignature = getSignatureOfTypeTag(signature.declaration!);
if (jsdocSignature && signature !== jsdocSignature) {
jsdocPredicate = getTypePredicateOfSignature(jsdocSignature);
@@ -17470,8 +17475,7 @@ namespace ts {
}
function isContextSensitiveFunctionLikeDeclaration(node: FunctionLikeDeclaration): boolean {
return (!isFunctionDeclaration(node) || isInJSFile(node) && !!getTypeForDeclarationFromJSDocComment(node)) &&
(hasContextSensitiveParameters(node) || hasContextSensitiveReturnExpression(node));
return hasContextSensitiveParameters(node) || hasContextSensitiveReturnExpression(node);
}
function hasContextSensitiveReturnExpression(node: FunctionLikeDeclaration) {
@@ -17480,7 +17484,7 @@ namespace ts {
}
function isContextSensitiveFunctionOrObjectLiteralMethod(func: Node): func is FunctionExpression | ArrowFunction | MethodDeclaration {
return (isInJSFile(func) && isFunctionDeclaration(func) || isFunctionExpressionOrArrowFunction(func) || isObjectLiteralMethod(func)) &&
return (isFunctionExpressionOrArrowFunction(func) || isObjectLiteralMethod(func)) &&
isContextSensitiveFunctionLikeDeclaration(func);
}