arktype
arktype copied to clipboard
Mapped types
I created the following props rule type to support intersections/unions and eventually checking types whose values are dependent on associated keys. Some parts of this may end up being out of date, but would be a good basis to start with.
const mappedKeyRegex = /^\[.*\]$/
const isMappedKey = (propKey: string) => mappedKeyRegex.test(propKey)
export type TraversalMappedPropsRule = [
mappedEntries: readonly TraversalMappedPropEntry[],
namedProps?: {
readonly required?: TraversalPropSet
readonly optional?: TraversalPropSet
}
]
export type TraversalMappedPropEntry = [
ifKeySatisfies: TraversalNode,
thenCheckValueAgainst: TraversalNode | ((key: string) => TraversalNode)
]
export type TraversalPropSet = { readonly [propKey in string]: TraversalNode }