Skip to content

Commit

Permalink
Resolve type parameters on arrow-methods
Browse files Browse the repository at this point in the history
Resolves #2320
  • Loading branch information
Gerrit0 committed Dec 27, 2023
1 parent 03426c3 commit e2160f4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
27 changes: 24 additions & 3 deletions src/lib/converter/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 23 additions & 0 deletions src/test/converter2/issues/gh2320.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
export type BaseUnionMember = {
type: string;
};

export type Union =
| {
type: "one";
}
| {
type: "two";
};

export class GenericClass<U extends BaseUnionMember> {
public arrowFunction = <MemberType extends U["type"]>(
member: Extract<U, { type: MemberType }>,
) => {};

public classFunction<MemberType extends U["type"]>(
member: Extract<U, { type: MemberType }>,
) {}
}

export class ResolvedSubclass extends GenericClass<Union> {}
7 changes: 7 additions & 0 deletions src/test/issues.c2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit e2160f4

Please sign in to comment.