Skip to content

Commit c245ede

Browse files
authored
Support type arguments for generated types (#72)
1 parent 15eb409 commit c245ede

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

type-generation/src/astToIR.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
SourceFile,
1616
SyntaxKind,
1717
ts,
18+
TypeElementMemberedNode,
1819
TypeElementTypes,
1920
TypeLiteralNode,
2021
TypeNode,
@@ -365,9 +366,15 @@ class SyntheticTypeConverter {
365366
}
366367

367368
doConversion(
368-
nodes: Pick<TypeLiteralNode, "getMembers">[],
369+
nodes: (TypeElementMemberedNode & Node)[],
369370
modifiers: Modifier,
370371
): ReferenceTypeIR {
372+
const res = nodes.filter(
373+
(x): x is TypeElementMemberedNode & TypeParameteredNode & Node =>
374+
Node.isTypeParametered(x),
375+
);
376+
const typeParams = this.converter.getTypeParamsFromDecls(res);
377+
371378
let name = this.nameContext.join("__");
372379
if (!name.endsWith("_iface")) {
373380
name += "_iface";
@@ -384,7 +391,14 @@ class SyntheticTypeConverter {
384391
(x) => !Node.isPropertyNamed(x) || pickSet.has(x.getName()),
385392
);
386393
}
387-
const result = this.converter.interfaceToIR(name, [], members, [], [], []);
394+
const result = this.converter.interfaceToIR(
395+
name,
396+
[],
397+
members,
398+
[],
399+
[],
400+
typeParams,
401+
);
388402
if (partial) {
389403
for (const prop of result.properties) {
390404
prop.isOptional = true;
@@ -501,10 +515,6 @@ class SyntheticTypeConverter {
501515
}
502516
if (res.kind === "interfaces") {
503517
const { name, ifaces } = res;
504-
const typeParams = this.converter.getTypeParamsFromDecls(ifaces);
505-
if (typeParams.length > 0) {
506-
throw new Error("Not handled");
507-
}
508518
this.nameContext.push(name);
509519
const result = this.doConversion(ifaces, modifiers);
510520
this.nameContext.pop();

type-generation/tests/a.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,6 +1787,24 @@ describe("emit", () => {
17871787
`).trim(),
17881788
);
17891789
});
1790+
it("PartialTypeArg", () => {
1791+
const res = emitFile(`
1792+
interface A<T> { a: T; }
1793+
type D = Partial<A<string>>;
1794+
declare function f(): D;
1795+
`);
1796+
assert.strictEqual(
1797+
removeTypeIgnores(res.slice(1).join("\n\n")),
1798+
dedent(`
1799+
type D = D__Partial__A_iface
1800+
1801+
def f() -> D: ...
1802+
1803+
class D__Partial__A_iface[T](Protocol):
1804+
a: T | None = ...
1805+
`).trim(),
1806+
);
1807+
});
17901808
});
17911809
it("Composed type operators", () => {
17921810
const res = emitFile(`

0 commit comments

Comments
 (0)