arktype
arktype copied to clipboard
Support recursive generics in `2.0` release candidate
Recursive generics work at a type-level, but will crash at runtime.
We need to introduce a mechanism for tracking instantiated args so we can avoid infinitely recursing in these cases.
Also would mean re-enabling these tests in generic.test.ts
it("self-reference", () => {
const types = scope({
"alternate<a, b>": {
// ensures old generic params aren't intersected with
// updated values (would be never)
swap: "alternate<b, a>",
order: ["a", "b"],
},
reference: "alternate<0, 1>",
}).export();
attest<[0, 1]>(types.reference.infer.swap.swap.order);
attest<[1, 0]>(types.reference.infer.swap.swap.swap.order);
const fromCall = types.alternate("'off'", "'on'");
attest<["off", "on"]>(fromCall.infer.swap.swap.order);
attest<["on", "off"]>(fromCall.infer.swap.swap.swap.order);
});
it("self-reference no params", () => {
attest(() =>
scope({
"nest<t>": {
// @ts-expect-error
nest: "nest",
},
}).export()
).throwsAndHasTypeError(writeInvalidGenericArgsMessage("nest", ["t"], []));
});