fix(46611): allow to use jsdoc type on class methods (#46688)

This commit is contained in:
Oleksandr T
2022-02-10 19:17:30 +02:00
committed by GitHub
parent df673f74f5
commit 954ce5b278
4 changed files with 52 additions and 6 deletions

View File

@@ -8773,12 +8773,8 @@ namespace ts {
}
}
if (isInJSFile(declaration)) {
const typeTag = getJSDocType(func);
if (typeTag && isFunctionTypeNode(typeTag)) {
const signature = getSignatureFromDeclaration(typeTag);
const pos = func.parameters.indexOf(declaration);
return declaration.dotDotDotToken ? getRestTypeAtPosition(signature, pos) : getTypeAtPosition(signature, pos);
}
const type = getParameterTypeOfTypeTag(func, declaration);
if (type) return type;
}
// Use contextual parameter type if one is available
const type = declaration.symbol.escapedName === InternalSymbolName.This ? getContextualThisParameterType(func) : getContextuallyTypedParameterType(declaration);
@@ -12776,6 +12772,13 @@ namespace ts {
return typeTag?.typeExpression && getSingleCallSignature(getTypeFromTypeNode(typeTag.typeExpression));
}
function getParameterTypeOfTypeTag(func: FunctionLikeDeclaration, parameter: ParameterDeclaration) {
const signature = getSignatureOfTypeTag(func);
if (!signature) return undefined;
const pos = func.parameters.indexOf(parameter);
return parameter.dotDotDotToken ? getRestTypeAtPosition(signature, pos) : getTypeAtPosition(signature, pos);
}
function getReturnTypeOfTypeTag(node: SignatureDeclaration | JSDocSignature) {
const signature = getSignatureOfTypeTag(node);
return signature && getReturnTypeOfSignature(signature);

View File

@@ -0,0 +1,15 @@
=== tests/cases/conformance/jsdoc/test.js ===
/**
* @typedef {(a: string, b: number) => void} Foo
*/
class C {
>C : Symbol(C, Decl(test.js, 0, 0))
/** @type {Foo} */
foo(a, b) {}
>foo : Symbol(C.foo, Decl(test.js, 4, 9))
>a : Symbol(a, Decl(test.js, 6, 8))
>b : Symbol(b, Decl(test.js, 6, 10))
}

View File

@@ -0,0 +1,15 @@
=== tests/cases/conformance/jsdoc/test.js ===
/**
* @typedef {(a: string, b: number) => void} Foo
*/
class C {
>C : C
/** @type {Foo} */
foo(a, b) {}
>foo : (a: string, b: number) => void
>a : string
>b : number
}

View File

@@ -0,0 +1,13 @@
// @checkJs: true
// @allowJs: true
// @noEmit: true
// @Filename: test.js
/**
* @typedef {(a: string, b: number) => void} Foo
*/
class C {
/** @type {Foo} */
foo(a, b) {}
}