diff --git a/CHANGELOG.md b/CHANGELOG.md index 11688d22a..6829e8e0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ ### Bug Fixes +- Type parameters will now be resolved for arrow-methods on classes like regular class methods, #2320. - Methods which return function types no longer have duplicated comments, #2336. - Comments on function-like type aliases will now show up under the type alias, rather than nested within the type declaration, #2372. - Fix crash when converting some complicated union/intersection types, #2451. diff --git a/src/lib/converter/symbols.ts b/src/lib/converter/symbols.ts index bbceb26bb..db0eafebf 100644 --- a/src/lib/converter/symbols.ts +++ b/src/lib/converter/symbols.ts @@ -748,10 +748,31 @@ function convertArrowAsMethod( const rc = context.withScope(reflection); - const signature = context.checker.getSignatureFromDeclaration(arrow); - assert(signature); + const locationDeclaration = + symbol.parent + ?.getDeclarations() + ?.find( + (d) => ts.isClassDeclaration(d) || ts.isInterfaceDeclaration(d), + ) ?? + symbol.parent?.getDeclarations()?.[0]?.getSourceFile() ?? + symbol.getDeclarations()?.[0]?.getSourceFile(); + assert(locationDeclaration, "Missing declaration context"); - createSignature(rc, ReflectionKind.CallSignature, signature, symbol, arrow); + const type = context.checker.getTypeOfSymbolAtLocation( + symbol, + locationDeclaration, + ); + + const signatures = type.getNonNullableType().getCallSignatures(); + assert(signatures.length, "Missing signatures"); + + createSignature( + rc, + ReflectionKind.CallSignature, + signatures[0], + symbol, + arrow, + ); } function convertConstructor(context: Context, symbol: ts.Symbol) { diff --git a/src/test/converter2/issues/gh2320.ts b/src/test/converter2/issues/gh2320.ts new file mode 100644 index 000000000..5e845f601 --- /dev/null +++ b/src/test/converter2/issues/gh2320.ts @@ -0,0 +1,23 @@ +export type BaseUnionMember = { + type: string; +}; + +export type Union = + | { + type: "one"; + } + | { + type: "two"; + }; + +export class GenericClass { + public arrowFunction = ( + member: Extract, + ) => {}; + + public classFunction( + member: Extract, + ) {} +} + +export class ResolvedSubclass extends GenericClass {} diff --git a/src/test/issues.c2.test.ts b/src/test/issues.c2.test.ts index 252439402..8fcb8a56f 100644 --- a/src/test/issues.c2.test.ts +++ b/src/test/issues.c2.test.ts @@ -1123,6 +1123,13 @@ describe("Issue Tests", () => { equal(tp.flags.isConst, true); }); + it("Uses type parameters from parent class in arrow-methods, #2320", () => { + const project = convert(); + const arrow = querySig(project, "ResolvedSubclass.arrowFunction"); + + equal(arrow.typeParameters![0].type?.toString(), '"one" | "two"'); + }); + it("Handles comments with nested methods #2336", () => { const project = convert();