Extract schema inside an array

Hello there, I'm new to ArkType, and I'm hitting an issue that I cannot seem to be able to solve... I'm trying to extract the runtime schema from within an array. Here is a simple example:
const simple = type({
id: 'number',
array: type({
name: 'string',
age: 'number',
}).array(),
});

const array = simple.get('array');
const arrayContent = array.get(0);
const res = arrayContent({ name: 'Rico', age: 25 });
const simple = type({
id: 'number',
array: type({
name: 'string',
age: 'number',
}).array(),
});

const array = simple.get('array');
const arrayContent = array.get(0);
const res = arrayContent({ name: 'Rico', age: 25 });
The type of res is correctly inferred to
const res: {
name: string;
age: number;
} | ArkErrors
const res: {
name: string;
age: number;
} | ArkErrors
However, the last line is always crashing with TypeError: Cannot convert undefined or null to object it seems the runtime schema is not correctly extracted. I know this was referred as an "anti-pattern" in the docs but here I'm trying to build a tool that can generate an openapi spec out of an ArkType definition so I cannot split my type. Any help would be appreciated.
8 Replies
Dimava
Dimava4d ago
Maybe you need some .arrayitem as .get(0) is for tuples?
Nicolas
NicolasOP4d ago
Thanks, where should I use this .arrayitem? I cannot find it in the docs
Dimava
Dimava4d ago
Idk how it's called check the sauce or runtime
Nicolas
NicolasOP4d ago
Okay I'll have a look deeper
Dimava
Dimava4d ago
I just remember David talking about it some time Re: OpenAPI 1. There is .json() that returns sorta schema you may want it 2. Lurk this server you're not the first to openapi
Nicolas
NicolasOP4d ago
Yes, my example is simplified but I want to use .toJsonSchema() on this inner array type but since the runtime value seems not out, I cannot do it
Dimava
Dimava4d ago
Not toJsonSchema toJsonInternalRepresentation .json iirc
Nicolas
NicolasOP3d ago
I opened an issue, it seems to be a bug

Did you find this page helpful?