Skip to content

Commit 954ce5b

Browse files
authored
fix(46611): allow to use jsdoc type on class methods (#46688)
1 parent df673f7 commit 954ce5b

File tree

4 files changed

+52
-6
lines changed

4 files changed

+52
-6
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8773,12 +8773,8 @@ namespace ts {
87738773
}
87748774
}
87758775
if (isInJSFile(declaration)) {
8776-
const typeTag = getJSDocType(func);
8777-
if (typeTag && isFunctionTypeNode(typeTag)) {
8778-
const signature = getSignatureFromDeclaration(typeTag);
8779-
const pos = func.parameters.indexOf(declaration);
8780-
return declaration.dotDotDotToken ? getRestTypeAtPosition(signature, pos) : getTypeAtPosition(signature, pos);
8781-
}
8776+
const type = getParameterTypeOfTypeTag(func, declaration);
8777+
if (type) return type;
87828778
}
87838779
// Use contextual parameter type if one is available
87848780
const type = declaration.symbol.escapedName === InternalSymbolName.This ? getContextualThisParameterType(func) : getContextuallyTypedParameterType(declaration);
@@ -12776,6 +12772,13 @@ namespace ts {
1277612772
return typeTag?.typeExpression && getSingleCallSignature(getTypeFromTypeNode(typeTag.typeExpression));
1277712773
}
1277812774

12775+
function getParameterTypeOfTypeTag(func: FunctionLikeDeclaration, parameter: ParameterDeclaration) {
12776+
const signature = getSignatureOfTypeTag(func);
12777+
if (!signature) return undefined;
12778+
const pos = func.parameters.indexOf(parameter);
12779+
return parameter.dotDotDotToken ? getRestTypeAtPosition(signature, pos) : getTypeAtPosition(signature, pos);
12780+
}
12781+
1277912782
function getReturnTypeOfTypeTag(node: SignatureDeclaration | JSDocSignature) {
1278012783
const signature = getSignatureOfTypeTag(node);
1278112784
return signature && getReturnTypeOfSignature(signature);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/conformance/jsdoc/test.js ===
2+
/**
3+
* @typedef {(a: string, b: number) => void} Foo
4+
*/
5+
6+
class C {
7+
>C : Symbol(C, Decl(test.js, 0, 0))
8+
9+
/** @type {Foo} */
10+
foo(a, b) {}
11+
>foo : Symbol(C.foo, Decl(test.js, 4, 9))
12+
>a : Symbol(a, Decl(test.js, 6, 8))
13+
>b : Symbol(b, Decl(test.js, 6, 10))
14+
}
15+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/conformance/jsdoc/test.js ===
2+
/**
3+
* @typedef {(a: string, b: number) => void} Foo
4+
*/
5+
6+
class C {
7+
>C : C
8+
9+
/** @type {Foo} */
10+
foo(a, b) {}
11+
>foo : (a: string, b: number) => void
12+
>a : string
13+
>b : number
14+
}
15+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// @checkJs: true
2+
// @allowJs: true
3+
// @noEmit: true
4+
// @Filename: test.js
5+
6+
/**
7+
* @typedef {(a: string, b: number) => void} Foo
8+
*/
9+
10+
class C {
11+
/** @type {Foo} */
12+
foo(a, b) {}
13+
}

0 commit comments

Comments
 (0)