@jsonjoy.com/json-type
Version:
High-performance JSON Pointer implementation
50 lines (49 loc) • 2.73 kB
TypeScript
import * as schema from '../../schema';
import type { ExcludeFromTuple, PickFromTuple } from '../../util/types';
import type { SchemaOf, SchemaOfObjectFields, Type } from '../types';
import { AbsType } from './AbsType';
export declare class KeyType<K extends string, V extends Type> extends AbsType<schema.KeySchema<K, SchemaOf<V>>> {
readonly key: K;
readonly val: V;
readonly optional: boolean;
constructor(key: K, val: V);
getSchema(): schema.KeySchema<K, SchemaOf<V>>;
getOptions(): schema.Optional<schema.KeySchema<K, SchemaOf<V>>>;
protected toStringTitle(): string;
toString(tab?: string): string;
}
export declare class KeyOptType<K extends string, V extends Type> extends KeyType<K, V> {
readonly key: K;
readonly val: V;
readonly optional: boolean;
constructor(key: K, val: V);
protected toStringTitle(): string;
}
export declare class ObjType<F extends (KeyType<any, any> | KeyOptType<any, any>)[] = (KeyType<any, any> | KeyOptType<any, any>)[]> extends AbsType<schema.ObjSchema<SchemaOfObjectFields<F>>> {
readonly keys: F;
constructor(keys: F);
private _key;
/**
* Adds a property to the object type.
* @param key The key of the property.
* @param value The value type of the property.
* @param options Optional schema options for the property.
* @returns A new object type with the added property.
*/
prop<K extends string, V extends Type>(key: K, value: V, options?: schema.Optional<schema.KeySchema<K, SchemaOf<V>>>): ObjType<[...F, KeyType<K, V>]>;
/**
* Adds an optional property to the object type.
* @param key The key of the property.
* @param value The value type of the property.
* @param options Optional schema options for the property.
* @returns A new object type with the added property.
*/
opt<K extends string, V extends Type>(key: K, value: V, options?: schema.Optional<schema.KeySchema<K, SchemaOf<V>>>): ObjType<[...F, KeyOptType<K, V>]>;
getSchema(): schema.ObjSchema<SchemaOfObjectFields<F>>;
getOptions(): schema.Optional<schema.ObjSchema<SchemaOfObjectFields<F>>>;
getField<K extends keyof schema.TypeOf<schema.ObjSchema<SchemaOfObjectFields<F>>>>(key: K): KeyType<string, Type> | undefined;
extend<F2 extends KeyType<any, any>[]>(o: ObjType<F2>): ObjType<[...F, ...F2]>;
omit<K extends keyof schema.TypeOf<schema.ObjSchema<SchemaOfObjectFields<F>>>>(key: K): ObjType<ExcludeFromTuple<F, KeyType<K extends string ? K : never, any>>>;
pick<K extends keyof schema.TypeOf<schema.ObjSchema<SchemaOfObjectFields<F>>>>(key: K): ObjType<PickFromTuple<F, KeyType<K extends string ? K : never, any>>>;
toString(tab?: string): string;
}