arktype
arktype copied to clipboard
Morph keys
Given a definition like:
const $ = scope({
a: ["'a'", "=>", () => "b"],
o: {
"[a]": "string",
},
});
We could parse o
as an object of with morph type (In: {a: string}) => Out<{b: string}>
.
Currently, this fails semantic validation as the morph types are not extracted.
Hi, in addition to it. Is this possible? Not sure if it's related.
@viniciusflv This isn't really related to moprh keys. Luckily, it should actually be doable already!
The only runtime issue with the definition is that for an index signature, the brackets need to be in the key itself:
const attributes = type("<T extends string>", {"[T]": "string"})(camelCaseRegex)
If you're just using the generic to pass the regex, you can actually inline it if you prefer:
const t = type({"[/regexHere/]": "string"})
Note also that if you don't want to allow any keys that don't match that pattern, you'll want to add a "+": "reject"
or "+": "delete"
key to strip them.
It works!!! Thanks 😊 Just curious. Why is this rejection not a default behavior?
-
It's how TypeScript works, which is good because...
-
Checking for extraneous keys is much slower
-
Usually they can be safely ignored