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 }
Here's an example of how the syntax might be used (thanks @Dimava):
const actualRecordScope = scope({
index: '1 | 3 | 5 | 7',
record: {
'[K in index]': 'index'
}
})
I have a dirty proposal of clear difference between finite and infinite records:
const actualRecordScope = scope({
finiteIndex: '1 | 3 | 5 | 7',
finiteRecord: {
'[K of finiteIndex]': 'index' // OF for every-key-of-type, requires type to be able to list all its values
},
infiniteIndex: /^\w+$/,
infiniteRecord: {
'[K in infiniteIndex]': 'index' // IN
},
})