dibbo
dibbo
Aarktype
Created by dibbo on 8/29/2024 in #questions
Optional property with const string array
Oh nice, I didn’t know that was an option. Thank you for the replies
6 replies
Aarktype
Created by dibbo on 8/27/2024 in #questions
Indexed Access Types
Nice, thank you for the help!
9 replies
Aarktype
Created by dibbo on 8/27/2024 in #questions
Indexed Access Types
Thank you! Good question... I've been looking at this for so long I can't remember 😂 Let me give a more detailed example: I'm trying to create a type to be used to validate the payload in a search request. I've landed on this as the implementation
const status = ["Available", "Unavailable", "Sold"] as const;
const size = [
{ label: "Big", value: "100kg" },
{ label: "Small", value: "10kg" },
] as const;

const searchPayload = type({
"page?": "number",
"sort?": "string",
filters: {
"status?": type(["===", ...status]).array(),
"size?": type(["===", ...size.map((s) => s.value)]).array(),
},
});

type SearchPayload = typeof searchPayload.infer;
// Evaluates to the type I want
// type SearchPayload = {
// filters: {
// status?: ("Available" | "Unavailable" | "Sold")[];
// size?: ("100kg" | "10kg")[];
// };
// page?: number;
// sort?: string;
// }
const status = ["Available", "Unavailable", "Sold"] as const;
const size = [
{ label: "Big", value: "100kg" },
{ label: "Small", value: "10kg" },
] as const;

const searchPayload = type({
"page?": "number",
"sort?": "string",
filters: {
"status?": type(["===", ...status]).array(),
"size?": type(["===", ...size.map((s) => s.value)]).array(),
},
});

type SearchPayload = typeof searchPayload.infer;
// Evaluates to the type I want
// type SearchPayload = {
// filters: {
// status?: ("Available" | "Unavailable" | "Sold")[];
// size?: ("100kg" | "10kg")[];
// };
// page?: number;
// sort?: string;
// }
I was wondering if there was a cleaner/preferred way to pick out the value prop from the size array with arktype, rather than creating a new array via the map.
9 replies
Aarktype
Created by dibbo on 8/16/2024 in #questions
Latest "keys:distilled" equivalent?
No problem at all, thank you for the (impressively fast) response! 👍
11 replies