Recursive types and d.ts files

I'm trying to define recursive types, where there's different kind of objects. One kind is a container, which can contain any kind of those objects, including other containers. My current approach looks like this:
export const TestData = type.module({
objectData: "object | container",
object: {
type: "'object'",
},
container: {
type: "'container'",
content: "objectData[]",
},
});
export type TestData = typeof TestData.objectData.infer;
export const TestData = type.module({
objectData: "object | container",
object: {
type: "'object'",
},
container: {
type: "'container'",
content: "objectData[]",
},
});
export type TestData = typeof TestData.objectData.infer;
I'm then trying to use the inferred type to define objects in TS. This works great when doing it inside the project where I create the types and I get errors/hints/etc. (see image 1). I also build the project into d.ts file so they can be used in another project. However, when using the type in the other project, TS seems to add an any to the type information, so that I can add any content to the container. E.g. this raises no errors (also see image 2).
const foo: TestData = {
type: "container",
content: [
{
type: "unknown",
oh: "no",
},
],
const foo: TestData = {
type: "container",
content: [
{
type: "unknown",
oh: "no",
},
],
Is there any way to prevent this or other approaches to handle this? Like, I guess if I'd have a monorepo for those projects, I could probably import the TS files directly as a type, instead of relying on the d.ts files. Is this approach also possible/recommended if I intend to publish the package that contains the arttypes?
No description
No description
7 Replies
BozoWithTheBanjo
I get an error with your code version "arktype": "2.0.0-rc.33" Wild guess, does your tsconfig have strict: true?
No description
Sebaestschjin
SebaestschjinOP6d ago
It does yes. And I also get the error when using the type directly like in the example. Where I don't get the error, is when building the project (creating d.ts files) and the using projects references the d.ts files (instead of the .ts files).
ssalbdivad
ssalbdivad6d ago
Unfortunately this is a bug/limitation of TypeScript. TypeScript does not currently have a way to serialize anonymous cyclic types to .d.ts. If you're not able to resolve between your projects (e.g. within a monorepo) directly to .ts, my best recommendation would be: Log/+1 a relevant TS issue- @Andarist not sure if you happen to know off the top of your head if where this is tracked. Add support for explicitly declared scopes: https://github.com/arktypeio/arktype/issues/791
GitHub
Declared scopes · Issue #791 · arktypeio/arktype
Would add support for declaring the inferred types of an entire scope, with autocomplete for alias names. Similar to existing API for declared types (in beta). Maybe something like: declare<{ a:...
ssalbdivad
ssalbdivad6d ago
Another option would be precompiled type output for ArkType, which could potentially help eliminate the need to duplicate types but would be a bit more work to implement
Andarist
Andarist6d ago
Im not sure if this is tracked
Sebaestschjin
SebaestschjinOP5d ago
I feared as much, thanks for the clarification. 👍 I think I'll figure something out with a monorepo setup then.
Dimava
Dimava4d ago
You cannot export the recursive type itself, but you can export the definition type and use type.infer to compile it

Did you find this page helpful?