A
arktype•2mo ago
Sun

Creating array from another object

I'm new at arktype, just been trying to do UserSchema[]. I haven't seen anything on the docs, so how am I supposed to do arrays with types? What I have right now:
export const EventSchema = type({
title: "string",
description: "string | null",
participants: `{UserSchema}[]`, // Thought this would work because of the pattern of the library
happens_at: "Date",
created_by: UserSchema,
});
export const EventSchema = type({
title: "string",
description: "string | null",
participants: `{UserSchema}[]`, // Thought this would work because of the pattern of the library
happens_at: "Date",
created_by: UserSchema,
});
6 Replies
Sun
SunOP•2mo ago
I actually moved to this
export const EventSchema = type({
title: type.string,
description: type.string.or(type.null),
participants: `{UserSchema}[]`, // Thought this would work because of the pattern of the library
happens_at: type.Date,
created_by: UserSchema,
});
export const EventSchema = type({
title: type.string,
description: type.string.or(type.null),
participants: `{UserSchema}[]`, // Thought this would work because of the pattern of the library
happens_at: type.Date,
created_by: UserSchema,
});
Sorry, I didn't like the magic string that will eventually turn to this anyways
Apteryx
Apteryx•2mo ago
participants: UserSchema.array()
Sun
SunOP•2mo ago
thx! do people normally do it with the string notation instead of this?
Apteryx
Apteryx•2mo ago
i only started using arktype yesterday so idk 😆 but im liking the string notation, it matches typescript type syntax, is incredibly well typed, compat, and its vscode extension is a big plus
ssalbdivad
ssalbdivad•2mo ago
there's not really a way to embed type-safe objects in strings like this. maybe if TS eventually supports improved types for tagged template literals. if you don't like the string syntax anyways then yeah just use chaining here for sure. strings for cases like string | null are more canonical for the reasons @Apteryx mentions but there are multiple options for a reason
Sun
SunOP•2mo ago
I'm not using vscode, but even if it's highlighted it'd be... weird? dunno how to say it I understand it though

Did you find this page helpful?