arktype icon indicating copy to clipboard operation
arktype copied to clipboard

Mapped types

Open ssalbdivad opened this issue 1 year ago • 2 comments

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 }

ssalbdivad avatar Jan 23 '23 19:01 ssalbdivad