mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-30 04:16:48 -05:00
fix(46611): allow to use jsdoc type on class methods (#46688)
This commit is contained in:
@@ -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);
|
||||
|
||||
15
tests/baselines/reference/checkJsdocTypeTag7.symbols
Normal file
15
tests/baselines/reference/checkJsdocTypeTag7.symbols
Normal 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))
|
||||
}
|
||||
|
||||
15
tests/baselines/reference/checkJsdocTypeTag7.types
Normal file
15
tests/baselines/reference/checkJsdocTypeTag7.types
Normal 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
|
||||
}
|
||||
|
||||
13
tests/cases/conformance/jsdoc/checkJsdocTypeTag7.ts
Normal file
13
tests/cases/conformance/jsdoc/checkJsdocTypeTag7.ts
Normal 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) {}
|
||||
}
|
||||
Reference in New Issue
Block a user