Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions type-generation/src/astToIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,19 @@ class SyntheticTypeConverter {
switch (typeRoot.kind) {
case "intersection": {
const node = typeRoot.node;
for (const ty of node.getTypeNodes()) {
if (ty.getType().isTypeParameter()) {
// Can't intersect with a type parameter...
return ANY_IR;
}
if (
Node.isTypeReference(ty) &&
Node.isQualifiedName(ty.getTypeName())
) {
// We don't handle QualifiedNames yet
return ANY_IR;
}
}
const types = node
.getTypeNodes()
.map((ty, idx) => {
Expand All @@ -424,6 +437,11 @@ class SyntheticTypeConverter {
return res;
})
.filter((x): x is ReferenceTypeIR => !!x && x.kind === "reference");
for (const node of types) {
if (!node.name.endsWith("_iface")) {
node.name += "_iface";
}
}
const name = this.nameContext.join("__") + "_iface";
this.converter.extraTopLevels.push(
this.converter.interfaceToIR(name, types, [], [], [], []),
Expand Down Expand Up @@ -1453,6 +1471,7 @@ export function convertDecls(
.getBasesOfDecls(defs)
.filter((base) => base.name !== name);
const typeParams = converter.getTypeParamsFromDecls(defs);
converter.nameContext = [name];
const res = converter.interfaceToIR(
name,
baseNames,
Expand All @@ -1461,6 +1480,7 @@ export function convertDecls(
defs.flatMap((def) => def.getCallSignatures()),
typeParams,
);
converter.nameContext = undefined;
pushTopLevel(res);

// Clear interface type parameter constraints after processing interface
Expand Down
20 changes: 20 additions & 0 deletions type-generation/tests/a.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,26 @@ describe("emit", () => {
`).trim(),
);
});
it("type literal in interface", () => {
const res = emitFile(`
interface O {
x?: { a: string; };
};
declare function f(): O<string>;
`);
assert.strictEqual(
removeTypeIgnores(res.slice(1).join("\n\n")),
dedent(`
def f() -> O_iface[str]: ...

class O_iface(Protocol):
x: O_iface__x_iface | None = ...

class O_iface__x_iface(Protocol):
a: str = ...
`).trim(),
);
});
});
describe("adjustments", () => {
it("setTimeout", () => {
Expand Down