/** * GENERATED CODE - DO NOT MODIFY */ import { type ValidationResult } from '@atproto/lexicon' export type OmitKey = { [K2 in keyof T as K2 extends K ? never : K2]: T[K2] } export type $Typed = V & { $type: T } export type Un$Typed = OmitKey export type $Type = Hash extends 'main' ? Id : `${Id}#${Hash}` function isObject(v: V): v is V & object { return v != null && typeof v === 'object' } function is$type( $type: unknown, id: Id, hash: Hash, ): $type is $Type { return hash === 'main' ? $type === id : // $type === `${id}#${hash}` typeof $type === 'string' && $type.length === id.length + 1 + hash.length && $type.charCodeAt(id.length) === 35 /* '#' */ && $type.startsWith(id) && $type.endsWith(hash) } export type $TypedObject< V, Id extends string, Hash extends string, > = V extends { $type: $Type } ? V : V extends { $type?: string } ? V extends { $type?: infer T extends $Type } ? V & { $type: T } : never : V & { $type: $Type } export function is$typed( v: V, id: Id, hash: Hash, ): v is $TypedObject { return isObject(v) && '$type' in v && is$type(v.$type, id, hash) } export function maybe$typed( v: V, id: Id, hash: Hash, ): v is V & object & { $type?: $Type } { return ( isObject(v) && ('$type' in v ? v.$type === undefined || is$type(v.$type, id, hash) : true) ) } export type Validator = (v: unknown) => ValidationResult export type ValidatorParam = V extends Validator ? R : never /** * Utility function that allows to convert a "validate*" utility function into a * type predicate. */ export function asPredicate(validate: V) { return function (v: T): v is T & ValidatorParam { return validate(v).success } }