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
32 changes: 29 additions & 3 deletions type-generation/src/astToIR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,10 @@ type SyntheticTypeRoot =
| {
kind: "pick";
node: TypeReferenceNode;
}
| {
kind: "partial";
node: TypeReferenceNode;
};

function classifySyntheticType(node: TypeNode): SyntheticTypeRoot | undefined {
Expand All @@ -307,13 +311,17 @@ function classifySyntheticType(node: TypeNode): SyntheticTypeRoot | undefined {
if (name === "Omit") {
return { kind: "omit", node };
}
if (name === "Partial") {
return { kind: "partial", node };
}
if (name === "Pick") {
return { kind: "pick", node };
}
return undefined;
}

type Modifier = {
partial: boolean;
omitSet?: Set<string>;
pickSet?: Set<string>;
};
Expand All @@ -330,7 +338,10 @@ class SyntheticTypeConverter {
}

hasWork(base: TypeNode, modifiers: Modifier) {
const { omitSet, pickSet } = modifiers;
const { partial, omitSet, pickSet } = modifiers;
if (partial) {
return true;
}
if (omitSet) {
for (const prop of base.getType().getProperties()) {
if (omitSet.has(prop.getName())) {
Expand All @@ -357,7 +368,7 @@ class SyntheticTypeConverter {
name += "_iface";
}
let members = nodes.flatMap((x) => x.getMembers());
const { omitSet, pickSet } = modifiers;
const { omitSet, partial, pickSet } = modifiers;
if (omitSet) {
members = members.filter(
(x) => !Node.isPropertyNamed(x) || !omitSet.has(x.getName()),
Expand All @@ -369,6 +380,11 @@ class SyntheticTypeConverter {
);
}
const result = this.converter.interfaceToIR(name, [], members, [], [], []);
if (partial) {
for (const prop of result.properties) {
prop.isOptional = true;
}
}
this.converter.extraTopLevels.push(result);
return { kind: "reference", name, typeArgs: [] };
}
Expand All @@ -377,7 +393,7 @@ class SyntheticTypeConverter {
typeRoot: SyntheticTypeRoot,
modifiersArg?: Modifier,
): TypeIR {
let modifiers = modifiersArg ?? {};
let modifiers = modifiersArg ?? { partial: false };
switch (typeRoot.kind) {
case "intersection": {
const node = typeRoot.node;
Expand Down Expand Up @@ -426,6 +442,16 @@ class SyntheticTypeConverter {
this.nameContext.pop();
return res;
}
case "partial": {
const node = typeRoot.node;
const base = node.getTypeArguments()[0]!;
modifiers = structuredClone(modifiers);
modifiers.partial = true;
this.nameContext.push("Partial");
const result = this.typeToIR(base, modifiers);
this.nameContext.pop();
return result;
}
}
}

Expand Down
86 changes: 86 additions & 0 deletions type-generation/tests/a.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1696,6 +1696,92 @@ describe("emit", () => {
);
});
});
describe("Partial", () => {
it("Partial literal type", () => {
const res = emitFile(`
type B = Partial<{
s: boolean;
t: string;
}>;
declare function f(): B;
def f() -> B: ...
`);
assert.strictEqual(
removeTypeIgnores(res.slice(1).join("\n\n")),
dedent(`
type B = B__Partial_iface

def f() -> B: ...

class B__Partial_iface(Protocol):
s: bool | None = ...
t: str | None = ...
`).trim(),
);
});
it("PartialAlias", () => {
const res = emitFile(`
type A = {
s: boolean;
t: string;
};
type B = Partial<A>;
declare function f(): B;
`);
assert.strictEqual(
removeTypeIgnores(res.slice(1).join("\n\n")),
dedent(`
type B = B__Partial_iface

def f() -> B: ...

class B__Partial_iface(Protocol):
s: bool | None = ...
t: str | None = ...
`).trim(),
);
});
it("PartialInterface", () => {
const res = emitFile(`
interface A { a: string; b: string; }
type D = Partial<A>;
declare function f(): D;
`);
assert.strictEqual(
removeTypeIgnores(res.slice(1).join("\n\n")),
dedent(`
type D = D__Partial__A_iface

def f() -> D: ...

class D__Partial__A_iface(Protocol):
a: str | None = ...
b: str | None = ...
`).trim(),
);
});
});
it("Composed type operators", () => {
const res = emitFile(`
interface V {
i: string;
v: number[];
};
type M = Pick<Partial<V>, "v">
declare function f(): M;
`);
assert.strictEqual(
removeTypeIgnores(res.slice(1).join("\n\n")),
dedent(`
type M = M__Pick__Partial__V_iface

def f() -> M: ...

class M__Pick__Partial__V_iface(Protocol):
v: JsArray[int | float] | None = ...
`).trim(),
);
});
});
describe("adjustments", () => {
it("setTimeout", () => {
Expand Down