arktypea
arktype16mo ago
Dimava

`scope.validate` for a scope with imports

The usage of type.validate with custom existing scopes is straightforward - just use type.validate<def, scope.t>
However, how do I validate a new scope definition with existing imports/exports?
import { scope, type } from 'arktype'

const baseScope = scope({
  'id': 'string'
})
type $ = typeof baseScope.t

function myType<const def>(def: type.validate<def, $>): type.infer<def, $> {
  return baseScope.type(def)
}
myType({ x: 'id' })

function myScopeWithExport<const def>(def: scope.validate<def>): scope.infer<def> {
  return scope({
    ...baseScope.export(),
    ...def
  } as any) as any
}
myScopeWithExport({ x: 'id' })

function myScopeWithImport<const def>(def: scope.validate<def>): scope.infer<def> {
  return scope({
    ...baseScope.import(),
    ...def
  } as any) as any
}
myScopeWithImport({ x: 'id' })
Was this page helpful?