A
arktype4mo ago
dibbo

Optional property with const string array

Hello, the following
const x = ["a", "b", "c"] as const;
const myType = type({
"optional?": type("===", ...x),
});
const x = ["a", "b", "c"] as const;
const myType = type({
"optional?": type("===", ...x),
});
is giving a
Only required keys may specify default values
Only required keys may specify default values
error. It's not really clear to me why - I assume the === operator is doing more than what I think. Is there a way to have an optional property be one of a string array in arktype? Or is what I'm after effectively indexed access expressions? Thanks
GitHub
Issues · arktypeio/arktype
TypeScript's 1:1 validator, optimized from editor to runtime - Issues · arktypeio/arktype
3 Replies
PIat
PIat4mo ago
What if you do this?
const x = ["a", "b", "c"] as const;
const myType = type({
"optional?": ["===", ...x],
});
const x = ["a", "b", "c"] as const;
const myType = type({
"optional?": ["===", ...x],
});
ssalbdivad
ssalbdivad4mo ago
Yeah this error is actually just due to a quirk in how some previous versions resolved nested type calls. Updating to the 2.0.0-rc.3 or doing what @PIat suggests will resolve the issue I'd recommend @PIat's suggest as more canonical to avoid nesting type calls where possible Or you can just define the literal union separately then reference it
dibbo
dibboOP4mo ago
Oh nice, I didn’t know that was an option. Thank you for the replies
Want results from more Discord servers?
Add your server