UNPKG

macoolka-io

Version:

`macoolka-io` is Runtime type system for IO decoding/encoding.

59 lines (58 loc) 1.7 kB
/** * Collection for Common type * @desczh * 通用类型集合 * @file */ import * as t from 'io-ts'; import * as O from 'fp-ts/Option'; /** * apply a default value when the value is null. * @desczh * 给一个类型赋缺省值 * @example * import * as t from 'macoolka-io' * import { right } from 'fp-ts/Either' * * const M = t.withDefault(t.string, '123'); * expect(M.decode(null)).toEqual(right('123')); * expect(M.decode(null)).toEqual(right('123')); * expect(M.decode('4')).toEqual(right('4')); * * const MA=t.type({ * name:t.string, * names:t.array(t.string) * }) * * const MB=t.type({ * name:t.withDefault(t.string,'1'), * names:t.withDefault(t.array(t.string),[]) * }) * expect(MB.decode({})).toEqual(right({name:'1',names:[]})); * expect(MB.decode({name:'2'})).toEqual(right({name:'2',names:[]})); * expect(MB.decode({names:['1']})).toEqual(right({name:'1',names:['1']})); * expect(MB.decode({names:['3'],name:'3'})).toEqual(right({name:'3',names:['3']})); * expect(isLeft(MA.decode({}))).toEqual(true); * * @since 0.2.0 */ export declare const withDefault: <T extends t.Mixed>(type: T, defaultValue: t.OutputOf<T>) => t.Type<t.TypeOf<T>, t.OutputOf<T>, unknown>; /** * @ignore */ export declare function formatValue(v: any): string; /** * @since 0.2.0 */ export interface ShowMessage { showValue: (a: unknown) => O.Option<string>; showDecoder: (a: t.Decoder<any, any>) => O.Option<string>; showKey: (a: string) => O.Option<string>; } /** * Show Errors to string * @desczh * 格式化错误到文本 * @since 0.2.0 */ export declare const show: (as: t.Errors, showMessage?: Partial<ShowMessage>) => string;