schema key alias

Is there a way that I can create an alias to a key in the type? Like this:
import { type } from "arktype";

const thing = type({
"['quality' | 'q']": "number",
})
import { type } from "arktype";

const thing = type({
"['quality' | 'q']": "number",
})
7 Replies
ssalbdivad
ssalbdivad2mo ago
What behavior do you want when parsing 0, 1 or 2 of the kesy?
𝘵𝘳𝘢𝘮𝘱𝘰𝘹
it should be just one of, like in ts you can { quality: number; q: never } | { quality: never; q: number } maybe I can create a arktype generic that would result in this?
ssalbdivad
ssalbdivad2mo ago
Yeah you could do that with a generic. Maybe will add in a builtin xorProps at some point
𝘵𝘳𝘢𝘮𝘱𝘰𝘹
it should look like this? https://arktype.io/docs/generics#hkt
ArkType
ArkType Docs
TypeScript's 1:1 validator, optimized from editor to runtime
ssalbdivad
ssalbdivad2mo ago
You would probably have an easier time with the external wrapper version
𝘵𝘳𝘢𝘮𝘱𝘰𝘹
do you have any idea on how to implement this? I'm having a hard time understanding this part of the docs
ssalbdivad
ssalbdivad2mo ago
I need to start giving out vouchers for answering a limited number of these kinds of questions haha
import { type } from "arktype"

/** Force an operation like `{ a: 0 } & { b: 1 }` to be computed so that it displays `{ a: 0; b: 1 }`. */
export type show<t> = { [k in keyof t]: t[k] } & unknown

/** Either:
* A, with all properties of B undefined
* OR
* B, with all properties of A undefined
**/
export type propwiseXor<a, b> =
| show<a & { [k in keyof b]?: undefined }>
| show<b & { [k in keyof a]?: undefined }>

const xorProps = <
k1 extends string,
k2 extends string,
const v,
const rest,
t = show<
propwiseXor<{ [k in k1]: type.infer<v> }, { [k in k2]: type.infer<v> }> &
type.infer<rest>
>
>(
k1: k1,
k2: k2,
v: type.validate<v>,
rest: type.validate<rest> &
(type.infer<rest> extends object ? rest : "Must be an object")
): type<t> => {
const l = type.raw({ "...": rest, [k1]: v, [k2]: "never?" })

const r = type.raw({ "...": rest, [k2]: v, [k1]: "never?" })

return l.or(r) as never
}

const Obj = xorProps("a", "b", "number", { extraProp: "true" })
import { type } from "arktype"

/** Force an operation like `{ a: 0 } & { b: 1 }` to be computed so that it displays `{ a: 0; b: 1 }`. */
export type show<t> = { [k in keyof t]: t[k] } & unknown

/** Either:
* A, with all properties of B undefined
* OR
* B, with all properties of A undefined
**/
export type propwiseXor<a, b> =
| show<a & { [k in keyof b]?: undefined }>
| show<b & { [k in keyof a]?: undefined }>

const xorProps = <
k1 extends string,
k2 extends string,
const v,
const rest,
t = show<
propwiseXor<{ [k in k1]: type.infer<v> }, { [k in k2]: type.infer<v> }> &
type.infer<rest>
>
>(
k1: k1,
k2: k2,
v: type.validate<v>,
rest: type.validate<rest> &
(type.infer<rest> extends object ? rest : "Must be an object")
): type<t> => {
const l = type.raw({ "...": rest, [k1]: v, [k2]: "never?" })

const r = type.raw({ "...": rest, [k2]: v, [k1]: "never?" })

return l.or(r) as never
}

const Obj = xorProps("a", "b", "number", { extraProp: "true" })
(made a slight correction as I had forgotten to include extraProp in the result)

Did you find this page helpful?