Comparing version 0.0.1-11 to 0.0.1-12
@@ -5,3 +5,3 @@ import type { ArrayType, AtomicType, IntersectionType, ObjectType, OptionalType, RecordType, RefinedType, TupleType, Type, UnionType } from './type'; | ||
export declare const toString: () => string; | ||
export declare type __MediumTypeOf<TType, TMediumTypes> = TType extends ObjectType<infer TDefinition> ? __ObjectTypeDefinitionToMediumType<TDefinition, TMediumTypes> : TType extends RecordType<infer TKey, infer TValue> ? Record<__MediumTypeOfRecordKeyType<TKey, TMediumTypes>, __MediumTypeOf<TValue, TMediumTypes>> : TType extends ArrayType<infer TElementType> ? __MediumTypeOf<TElementType, TMediumTypes>[] : TType extends TupleType<infer TTuple> ? __TupleMediumType<TTuple, TMediumTypes> : TType extends RefinedType<infer TType, infer TNominal> ? __RefinedMediumType<TType, TNominal, TMediumTypes> : TType extends AtomicType<infer TTypeSymbol> ? __AtomicMediumType<TTypeSymbol, TMediumTypes> : TType extends UnionType<infer TTypeTuple> ? __MediumTypeOf<TTypeTuple[number], TMediumTypes> : TType extends IntersectionType<infer TTypeTuple> ? __UnionToIntersection<__MediumTypeOf<TTypeTuple[number], TMediumTypes>> : TType extends OptionalType<infer TType> ? __MediumTypeOf<TType, TMediumTypes> | undefined : never; | ||
export declare type __MediumTypeOf<TType, TMediumTypes> = TType extends ObjectType<infer TDefinition> ? __ObjectTypeDefinitionToMediumType<TDefinition, TMediumTypes> : TType extends RecordType<infer TKey, infer TValue> ? Record<__MediumTypeOfRecordKeyType<TKey, TMediumTypes>, __MediumTypeOf<TValue, TMediumTypes>> : TType extends ArrayType<infer TElementType> ? __MediumTypeOf<TElementType, TMediumTypes>[] : TType extends TupleType<infer TTuple> ? __TupleMediumType<TTuple, TMediumTypes> : TType extends RefinedType<infer TType, infer TRefined, infer TNominal> ? __RefinedMediumType<TType, TRefined, TNominal, TMediumTypes> : TType extends AtomicType<infer TTypeSymbol> ? __AtomicMediumType<TTypeSymbol, TMediumTypes> : TType extends UnionType<infer TTypeTuple> ? __MediumTypeOf<TTypeTuple[number], TMediumTypes> : TType extends IntersectionType<infer TTypeTuple> ? __UnionToIntersection<__MediumTypeOf<TTypeTuple[number], TMediumTypes>> : TType extends OptionalType<infer TType> ? __MediumTypeOf<TType, TMediumTypes> | undefined : never; | ||
export declare type __ObjectTypeDefinitionToMediumType<TDefinition, TMediumTypes> = { | ||
@@ -17,3 +17,3 @@ [TKey in __KeyOfOptional<TDefinition>]?: TDefinition[TKey] extends OptionalType<infer TNestedType> ? __MediumTypeOf<TNestedType, TMediumTypes> : never; | ||
}; | ||
export declare type __RefinedMediumType<TType, TNominal, TMediumTypes> = __MediumTypeOf<TType, TMediumTypes> extends infer T ? unknown extends TNominal ? T : T & TNominal & { | ||
export declare type __RefinedMediumType<TType, TRefined, TNominal, TMediumTypes> = __MediumTypeOf<TType, TMediumTypes> & TRefined extends infer T ? unknown extends TNominal ? T : T & TNominal & { | ||
[TNominalTypeSymbol in typeof __nominalType]: Denominalize<T>; | ||
@@ -35,2 +35,6 @@ } : never; | ||
export declare type __ElementOrArray<T> = T | T[]; | ||
export declare type __RefinedType<TType extends Type, TNominalOrRefined, TNominal> = RefinedType<TType, TNominalOrRefined extends __NominalPartial ? unknown : TNominalOrRefined, TNominalOrRefined extends __NominalPartial ? TNominalOrRefined : TNominal>; | ||
export declare type __NominalPartial = { | ||
[TNominalTypeSymbol in typeof __nominalType]: unknown; | ||
}; | ||
export declare function merge(partials: unknown[]): unknown; |
import type { MediumAtomicCodecs } from '../medium'; | ||
import { dateTypeSymbol, regexpTypeSymbol } from '../types'; | ||
import { bigintTypeSymbol, dateTypeSymbol, regexpTypeSymbol } from '../types'; | ||
export interface ExtendedTypes { | ||
[bigintTypeSymbol]: string; | ||
[dateTypeSymbol]: string; | ||
@@ -5,0 +6,0 @@ [regexpTypeSymbol]: string; |
@@ -8,2 +8,13 @@ "use strict"; | ||
exports.EXTENDED_CODECS = { | ||
[types_1.bigintTypeSymbol]: { | ||
encode(bigint) { | ||
return bigint.toString(); | ||
}, | ||
decode(value) { | ||
if (typeof value !== 'string') { | ||
throw new TypeError(`Expected bigint string, getting ${_utils_1.toString.call(value)}`); | ||
} | ||
return BigInt(value); | ||
}, | ||
}, | ||
[types_1.dateTypeSymbol]: { | ||
@@ -10,0 +21,0 @@ encode(date) { |
@@ -1,2 +0,2 @@ | ||
import type { booleanTypeSymbol, dateTypeSymbol, nullTypeSymbol, numberTypeSymbol, stringTypeSymbol, undefinedTypeSymbol, unknownTypeSymbol, voidTypeSymbol } from '../types'; | ||
import type { bigintTypeSymbol, booleanTypeSymbol, dateTypeSymbol, nullTypeSymbol, numberTypeSymbol, regexpTypeSymbol, stringTypeSymbol, undefinedTypeSymbol, unknownTypeSymbol, voidTypeSymbol } from '../types'; | ||
export interface ECMAScriptTypes { | ||
@@ -9,5 +9,7 @@ [unknownTypeSymbol]: unknown; | ||
[numberTypeSymbol]: number; | ||
[bigintTypeSymbol]: bigint; | ||
[booleanTypeSymbol]: boolean; | ||
[dateTypeSymbol]: Date; | ||
[regexpTypeSymbol]: RegExp; | ||
} | ||
export declare const ecmascript: import("../medium").Medium<ECMAScriptTypes>; |
@@ -1,9 +0,7 @@ | ||
import type { __ElementOrArray, __MediumTypeOf, __MediumTypesPackedType } from '../@utils'; | ||
import type { __ElementOrArray, __MediumTypeOf, __MediumTypesPackedType, __RefinedType } from '../@utils'; | ||
import type { Medium } from '../medium'; | ||
import type { Nominal } from '../utils'; | ||
import type { RefinedType } from './refined-type'; | ||
import type { TypeConstraint, TypeOf } from './type'; | ||
import { Type } from './type'; | ||
export interface ArrayType<TElement> { | ||
refine<TNominal>(constraints: __ElementOrArray<TypeConstraint<TypeOf<TElement>[]>>): RefinedType<this, Nominal<TNominal>>; | ||
refine<TNominalOrRefined, TNominal = unknown>(constraints: __ElementOrArray<TypeConstraint<TypeOf<TElement>[]>>): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
decode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __MediumTypesPackedType<TMediumTypes, __MediumTypeOf<TElement, TMediumTypes>[]>): TypeOf<TElement>[]; | ||
@@ -10,0 +8,0 @@ encode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: TypeOf<TElement>[]): __MediumTypesPackedType<TMediumTypes, __MediumTypeOf<TElement, TMediumTypes>[]>; |
@@ -1,5 +0,3 @@ | ||
import type { __AtomicMediumType, __ElementOrArray, __MediumTypesPackedType } from '../@utils'; | ||
import type { __AtomicMediumType, __ElementOrArray, __MediumTypesPackedType, __RefinedType } from '../@utils'; | ||
import type { Medium } from '../medium'; | ||
import type { Nominal } from '../utils'; | ||
import type { RefinedType } from './refined-type'; | ||
import type { TypeConstraint } from './type'; | ||
@@ -11,3 +9,3 @@ import { Type } from './type'; | ||
export interface AtomicType<TSymbol> { | ||
refine<TNominal>(constraints: __ElementOrArray<TypeConstraint<__AtomicMediumType<TSymbol, XValue.Types>>>): RefinedType<this, Nominal<TNominal>>; | ||
refine<TNominalOrRefined, TNominal = unknown>(constraints: __ElementOrArray<TypeConstraint<__AtomicMediumType<TSymbol, XValue.Types>>>): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
decode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __MediumTypesPackedType<TMediumTypes, __AtomicMediumType<TSymbol, TMediumTypes>>): __AtomicMediumType<TSymbol, XValue.Types>; | ||
@@ -14,0 +12,0 @@ encode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __AtomicMediumType<TSymbol, XValue.Types>): __MediumTypesPackedType<TMediumTypes, __AtomicMediumType<TSymbol, TMediumTypes>>; |
@@ -1,9 +0,7 @@ | ||
import type { __ElementOrArray, __MediumTypeOf, __MediumTypesPackedType, __UnionToIntersection } from '../@utils'; | ||
import type { __ElementOrArray, __MediumTypeOf, __MediumTypesPackedType, __RefinedType, __UnionToIntersection } from '../@utils'; | ||
import type { Medium } from '../medium'; | ||
import type { Nominal } from '../utils'; | ||
import type { RefinedType } from './refined-type'; | ||
import type { TypeConstraint, TypeOf } from './type'; | ||
import { Type } from './type'; | ||
export interface IntersectionType<TTypeTuple> { | ||
refine<TNominal>(constraints: __ElementOrArray<TypeConstraint<__UnionToIntersection<TypeOf<TTypeTuple[number]>>>>): RefinedType<this, Nominal<TNominal>>; | ||
refine<TNominalOrRefined, TNominal = unknown>(constraints: __ElementOrArray<TypeConstraint<__UnionToIntersection<TypeOf<TTypeTuple[number]>>>>): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
decode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __MediumTypesPackedType<TMediumTypes, __UnionToIntersection<__MediumTypeOf<TTypeTuple[number], TMediumTypes>>>): __UnionToIntersection<TypeOf<TTypeTuple[number]>>; | ||
@@ -10,0 +8,0 @@ encode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __UnionToIntersection<TypeOf<TTypeTuple[number]>>): __MediumTypesPackedType<TMediumTypes, __UnionToIntersection<__MediumTypeOf<TTypeTuple[number], TMediumTypes>>>; |
@@ -1,9 +0,7 @@ | ||
import type { __ElementOrArray, __MediumTypesPackedType, __ObjectTypeDefinitionToMediumType } from '../@utils'; | ||
import type { __ElementOrArray, __MediumTypesPackedType, __ObjectTypeDefinitionToMediumType, __RefinedType } from '../@utils'; | ||
import type { Medium } from '../medium'; | ||
import type { Nominal } from '../utils'; | ||
import type { RefinedType } from './refined-type'; | ||
import type { TypeConstraint } from './type'; | ||
import { Type } from './type'; | ||
export interface ObjectType<TTypeDefinition> { | ||
refine<TNominal>(constraints: __ElementOrArray<TypeConstraint<__ObjectTypeDefinitionToMediumType<TTypeDefinition, XValue.Types>>>): RefinedType<this, Nominal<TNominal>>; | ||
refine<TNominalOrRefined, TNominal = unknown>(constraints: __ElementOrArray<TypeConstraint<__ObjectTypeDefinitionToMediumType<TTypeDefinition, XValue.Types>>>): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
decode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, packed: __MediumTypesPackedType<TMediumTypes, __ObjectTypeDefinitionToMediumType<TTypeDefinition, TMediumTypes>>): __ObjectTypeDefinitionToMediumType<TTypeDefinition, XValue.Types>; | ||
@@ -10,0 +8,0 @@ encode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __ObjectTypeDefinitionToMediumType<TTypeDefinition, XValue.Types>): __MediumTypesPackedType<TMediumTypes, __ObjectTypeDefinitionToMediumType<TTypeDefinition, TMediumTypes>>; |
@@ -1,9 +0,7 @@ | ||
import type { __ElementOrArray, __MediumTypeOf, __MediumTypesPackedType } from '../@utils'; | ||
import type { __ElementOrArray, __MediumTypeOf, __MediumTypesPackedType, __RefinedType } from '../@utils'; | ||
import type { Medium } from '../medium'; | ||
import type { Nominal } from '../utils'; | ||
import type { RefinedType } from './refined-type'; | ||
import type { TypeConstraint, TypeOf } from './type'; | ||
import { Type } from './type'; | ||
export interface OptionalType<TType> { | ||
refine<TNominal>(constraints: __ElementOrArray<TypeConstraint<TypeOf<TType> | undefined>>): RefinedType<this, Nominal<TNominal>>; | ||
refine<TNominalOrRefined, TNominal = unknown>(constraints: __ElementOrArray<TypeConstraint<TypeOf<TType> | undefined>>): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
decode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __MediumTypesPackedType<TMediumTypes, __MediumTypeOf<TType, TMediumTypes> | undefined>): TypeOf<TType> | undefined; | ||
@@ -10,0 +8,0 @@ encode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: TypeOf<TType> | undefined): __MediumTypesPackedType<TMediumTypes, __MediumTypeOf<TType, TMediumTypes> | undefined>; |
@@ -1,9 +0,7 @@ | ||
import type { __ElementOrArray, __MediumTypeOf, __MediumTypeOfRecordKeyType, __MediumTypesPackedType, __TypeOfRecordKeyType } from '../@utils'; | ||
import type { __ElementOrArray, __MediumTypeOf, __MediumTypeOfRecordKeyType, __MediumTypesPackedType, __RefinedType, __TypeOfRecordKeyType } from '../@utils'; | ||
import type { Medium } from '../medium'; | ||
import type { Nominal } from '../utils'; | ||
import type { RefinedType } from './refined-type'; | ||
import type { TypeConstraint, TypeOf } from './type'; | ||
import { Type } from './type'; | ||
export interface RecordType<TKey, TValue> { | ||
refine<TNominal>(constraints: __ElementOrArray<TypeConstraint<Record<__TypeOfRecordKeyType<TKey>, TypeOf<TValue>>>>): RefinedType<this, Nominal<TNominal>>; | ||
refine<TNominalOrRefined, TNominal = unknown>(constraints: __ElementOrArray<TypeConstraint<Record<__TypeOfRecordKeyType<TKey>, TypeOf<TValue>>>>): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
decode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, packed: __MediumTypesPackedType<TMediumTypes, Record<__MediumTypeOfRecordKeyType<TKey, TMediumTypes>, __MediumTypeOf<TValue, TMediumTypes>>>): Record<__TypeOfRecordKeyType<TKey>, TypeOf<TValue>>; | ||
@@ -10,0 +8,0 @@ encode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: Record<__TypeOfRecordKeyType<TKey>, TypeOf<TValue>>): __MediumTypesPackedType<TMediumTypes, Record<__MediumTypeOfRecordKeyType<TKey, TMediumTypes>, __MediumTypeOf<TValue, TMediumTypes>>>; |
@@ -1,16 +0,16 @@ | ||
import type { __ElementOrArray, __MediumTypeOf, __MediumTypesPackedType, __RefinedMediumType } from '../@utils'; | ||
import type { __ElementOrArray, __MediumTypeOf, __MediumTypesPackedType, __RefinedMediumType, __RefinedType } from '../@utils'; | ||
import type { Medium } from '../medium'; | ||
import type { Nominal } from '../utils'; | ||
import type { TypeConstraint } from './type'; | ||
import { Type } from './type'; | ||
export interface RefinedType<TType, TNominal> { | ||
refine<TNominal>(constraints: __ElementOrArray<TypeConstraint<__RefinedMediumType<TType, TNominal, XValue.Types>>>): RefinedType<this, Nominal<TNominal>>; | ||
decode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __MediumTypesPackedType<TMediumTypes, __MediumTypeOf<TType, TMediumTypes> & TNominal>): __RefinedMediumType<TType, TNominal, XValue.Types>; | ||
encode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __RefinedMediumType<TType, TNominal, XValue.Types>): __MediumTypesPackedType<TMediumTypes, __MediumTypeOf<TType, TMediumTypes> & TNominal>; | ||
export interface RefinedType<TType, TRefined, TNominal> { | ||
refine<TNominalOrRefined, TNominal = unknown>(constraints: __ElementOrArray<TypeConstraint<__RefinedMediumType<TType, TNominalOrRefined, TNominal, XValue.Types>>>): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
decode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __MediumTypesPackedType<TMediumTypes, __MediumTypeOf<TType, TMediumTypes> & TNominal>): __RefinedMediumType<TType, TRefined, TNominal, XValue.Types>; | ||
encode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __RefinedMediumType<TType, TRefined, TNominal, XValue.Types>): __MediumTypesPackedType<TMediumTypes, __MediumTypeOf<TType, TMediumTypes> & TNominal>; | ||
transform<TFromMediumTypes extends object, TToMediumTypes extends object>(from: Medium<TFromMediumTypes>, to: Medium<TToMediumTypes>, value: __MediumTypesPackedType<TFromMediumTypes, __MediumTypeOf<TType, TFromMediumTypes> & TNominal>): __MediumTypesPackedType<TToMediumTypes, __MediumTypeOf<TType, TToMediumTypes> & TNominal>; | ||
is(value: unknown): value is __RefinedMediumType<TType, TNominal, XValue.Types>; | ||
is(value: unknown): value is __RefinedMediumType<TType, TRefined, TNominal, XValue.Types>; | ||
} | ||
export declare class RefinedType<TType extends Type, TNominal> extends Type<'refined'> { | ||
export declare class RefinedType<TType extends Type, TRefined, TNominal> extends Type<'refined'> { | ||
readonly Type: TType; | ||
readonly constraints: TypeConstraint[]; | ||
protected __static_type_refined: TRefined; | ||
protected __static_type_nominal: TNominal; | ||
@@ -20,2 +20,1 @@ constructor(Type: TType, constraints: TypeConstraint[]); | ||
} | ||
export declare function refined<TType extends Type, TNominal>(Type: TType, constraints: TypeConstraint | TypeConstraint[]): RefinedType<TType, TNominal>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.refined = exports.RefinedType = void 0; | ||
exports.RefinedType = void 0; | ||
const type_1 = require("./type"); | ||
@@ -20,2 +20,8 @@ class RefinedType extends type_1.Type { | ||
}); | ||
Object.defineProperty(this, "__static_type_refined", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: void 0 | ||
}); | ||
Object.defineProperty(this, "__static_type_nominal", { | ||
@@ -79,6 +85,2 @@ enumerable: true, | ||
exports.RefinedType = RefinedType; | ||
function refined(Type, constraints) { | ||
return new RefinedType(Type, Array.isArray(constraints) ? constraints : [constraints]); | ||
} | ||
exports.refined = refined; | ||
//# sourceMappingURL=refined-type.js.map |
@@ -1,9 +0,7 @@ | ||
import type { __ElementOrArray, __MediumTypesPackedType, __TupleMediumType } from '../@utils'; | ||
import type { __ElementOrArray, __MediumTypesPackedType, __RefinedType, __TupleMediumType } from '../@utils'; | ||
import type { Medium } from '../medium'; | ||
import type { Nominal } from '../utils'; | ||
import type { RefinedType } from './refined-type'; | ||
import type { TypeConstraint } from './type'; | ||
import { Type } from './type'; | ||
export interface TupleType<TElements> { | ||
refine<TNominal>(constraints: __ElementOrArray<TypeConstraint<__TupleMediumType<TElements, XValue.Types>>>): RefinedType<this, Nominal<TNominal>>; | ||
refine<TNominalOrRefined, TNominal = unknown>(constraints: __ElementOrArray<TypeConstraint<__TupleMediumType<TElements, XValue.Types>>>): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
decode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __MediumTypesPackedType<TMediumTypes, __TupleMediumType<TElements, TMediumTypes>>): __TupleMediumType<TElements, XValue.Types>; | ||
@@ -10,0 +8,0 @@ encode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: __TupleMediumType<TElements, XValue.Types>): __MediumTypesPackedType<TMediumTypes, __TupleMediumType<TElements, TMediumTypes>>; |
@@ -6,3 +6,3 @@ import type { __MediumTypeOf } from '../@utils'; | ||
protected __static_type_category: TCategory; | ||
refine(constraints: TypeConstraint | TypeConstraint[]): RefinedType<Type, Nominal<unknown>>; | ||
refine(constraints: TypeConstraint | TypeConstraint[]): RefinedType<Type, unknown, Nominal<unknown>>; | ||
decode(medium: Medium, packed: unknown): unknown; | ||
@@ -30,2 +30,2 @@ encode(medium: Medium, value: unknown): unknown; | ||
export declare type TypeOf<TType extends Type> = __MediumTypeOf<TType, XValue.Types>; | ||
import type { RefinedType } from './refined-type'; | ||
import { RefinedType } from './refined-type'; |
@@ -15,3 +15,3 @@ "use strict"; | ||
refine(constraints) { | ||
return (0, refined_type_1.refined)(this, constraints); | ||
return new refined_type_1.RefinedType(this, Array.isArray(constraints) ? constraints : [constraints]); | ||
} | ||
@@ -87,3 +87,5 @@ decode(medium, packed) { | ||
exports.TypeConstraintError = TypeConstraintError; | ||
/* eslint-enable @mufan/import-groups */ | ||
// Make sure code in refined-type.ts accessing type.ts after exports ready. | ||
const refined_type_1 = require("./refined-type"); | ||
//# sourceMappingURL=type.js.map |
@@ -1,9 +0,7 @@ | ||
import type { __ElementOrArray, __MediumTypeOf, __MediumTypesPackedType } from '../@utils'; | ||
import type { __ElementOrArray, __MediumTypeOf, __MediumTypesPackedType, __RefinedType } from '../@utils'; | ||
import type { Medium } from '../medium'; | ||
import type { Nominal } from '../utils'; | ||
import type { RefinedType } from './refined-type'; | ||
import type { TypeConstraint, TypeOf } from './type'; | ||
import { Type } from './type'; | ||
export interface UnionType<TTypeTuple> { | ||
refine<TNominal>(constraints: __ElementOrArray<TypeConstraint<TypeOf<TTypeTuple[number]>>>): RefinedType<this, Nominal<TNominal>>; | ||
refine<TNominalOrRefined, TNominal = unknown>(constraints: __ElementOrArray<TypeConstraint<TypeOf<TTypeTuple[number]>>>): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
decode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, packed: __MediumTypesPackedType<TMediumTypes, __MediumTypeOf<TTypeTuple[number], TMediumTypes>>): TypeOf<TTypeTuple[number]>; | ||
@@ -10,0 +8,0 @@ encode<TMediumTypes extends object>(medium: Medium<TMediumTypes>, value: TypeOf<TTypeTuple[number]>): __MediumTypesPackedType<TMediumTypes, __MediumTypeOf<TTypeTuple[number], TMediumTypes>>; |
@@ -6,5 +6,7 @@ declare global { | ||
[undefinedTypeSymbol]: undefined; | ||
[voidTypeSymbol]: void; | ||
[nullTypeSymbol]: null; | ||
[stringTypeSymbol]: string; | ||
[numberTypeSymbol]: number; | ||
[bigintTypeSymbol]: bigint; | ||
[booleanTypeSymbol]: boolean; | ||
@@ -28,2 +30,4 @@ [dateTypeSymbol]: Date; | ||
export declare const number: import("./type").AtomicType<typeof numberTypeSymbol>; | ||
export declare const bigintTypeSymbol: unique symbol; | ||
export declare const bigint: import("./type").AtomicType<typeof bigintTypeSymbol>; | ||
export declare const booleanTypeSymbol: unique symbol; | ||
@@ -30,0 +34,0 @@ export declare const boolean: import("./type").AtomicType<typeof booleanTypeSymbol>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RegExp = exports.regexpTypeSymbol = exports.Date = exports.dateTypeSymbol = exports.boolean = exports.booleanTypeSymbol = exports.number = exports.numberTypeSymbol = exports.string = exports.stringTypeSymbol = exports.nullType = exports.nullTypeSymbol = exports.voidType = exports.voidTypeSymbol = exports.undefined = exports.undefinedTypeSymbol = exports.unknown = exports.unknownTypeSymbol = void 0; | ||
exports.RegExp = exports.regexpTypeSymbol = exports.Date = exports.dateTypeSymbol = exports.boolean = exports.booleanTypeSymbol = exports.bigint = exports.bigintTypeSymbol = exports.number = exports.numberTypeSymbol = exports.string = exports.stringTypeSymbol = exports.nullType = exports.nullTypeSymbol = exports.voidType = exports.voidTypeSymbol = exports.undefined = exports.undefinedTypeSymbol = exports.unknown = exports.unknownTypeSymbol = void 0; | ||
const _utils_1 = require("./@utils"); | ||
@@ -26,2 +26,6 @@ const type_1 = require("./type"); | ||
: `Expected number, getting ${_utils_1.toString.call(value)}.`); | ||
exports.bigintTypeSymbol = Symbol(); | ||
exports.bigint = (0, type_1.atomic)(exports.bigintTypeSymbol, value => typeof value === 'bigint' | ||
? true | ||
: `Expected bigint, getting ${_utils_1.toString.call(value)}.`); | ||
exports.booleanTypeSymbol = Symbol(); | ||
@@ -28,0 +32,0 @@ exports.boolean = (0, type_1.atomic)(exports.booleanTypeSymbol, value => typeof value === 'boolean' |
import type { Type, TypeOf } from './type'; | ||
import { AtomicType, RefinedType } from './type'; | ||
import { booleanTypeSymbol, numberTypeSymbol, stringTypeSymbol, unknown } from './types'; | ||
import { RefinedType } from './type'; | ||
import { boolean, number, string, unknown } from './types'; | ||
/** | ||
@@ -18,3 +18,3 @@ * DECLARATION ONLY. | ||
export declare const __nominalType: unique symbol; | ||
export declare type Nominal<TNominal, T = unknown> = T & (unknown extends TNominal ? unknown : { | ||
export declare type Nominal<TNominal, T = unknown> = T & { | ||
[TNominalTypeSymbol in typeof __nominalType]: T; | ||
@@ -25,11 +25,11 @@ } & ([TNominal] extends [string | symbol] ? { | ||
}; | ||
} : TNominal)); | ||
} : TNominal); | ||
export declare type Denominalize<T> = T extends { | ||
[TNominalTypeSymbol in typeof __nominalType]: infer TDenominalized; | ||
} ? TDenominalized : T; | ||
export declare const UnknownRecord: import("./type").RecordType<AtomicType<typeof stringTypeSymbol>, AtomicType<typeof import("./types").unknownTypeSymbol>>; | ||
export declare function literal<T extends string>(literal: T): AtomicType<typeof stringTypeSymbol>; | ||
export declare function literal<T extends number>(literal: T): AtomicType<typeof numberTypeSymbol>; | ||
export declare function literal<T extends boolean>(literal: T): AtomicType<typeof booleanTypeSymbol>; | ||
export declare function equal(comparison: unknown): RefinedType<typeof unknown, unknown>; | ||
export declare function equal<TType extends Type>(comparison: TypeOf<TType>, Type: TType): RefinedType<TType, unknown>; | ||
export declare const UnknownRecord: import("./type").RecordType<import("./type").AtomicType<typeof import("./types").stringTypeSymbol>, import("./type").AtomicType<typeof import("./types").unknownTypeSymbol>>; | ||
export declare function literal<T extends string>(literal: T): RefinedType<typeof string, T, unknown>; | ||
export declare function literal<T extends number>(literal: T): RefinedType<typeof number, T, unknown>; | ||
export declare function literal<T extends boolean>(literal: T): RefinedType<typeof boolean, T, unknown>; | ||
export declare function equal<T>(comparison: T): RefinedType<typeof unknown, T, unknown>; | ||
export declare function equal<T extends TypeOf<TType>, TType extends Type>(comparison: T, Type: TType): RefinedType<TType, T, unknown>; |
@@ -10,17 +10,12 @@ "use strict"; | ||
function literal(literal) { | ||
let symbol; | ||
switch (typeof literal) { | ||
case 'string': | ||
symbol = types_1.stringTypeSymbol; | ||
break; | ||
return types_1.string.refine(value => value === literal); | ||
case 'number': | ||
symbol = types_1.numberTypeSymbol; | ||
break; | ||
return types_1.number.refine(value => value === literal); | ||
case 'boolean': | ||
symbol = types_1.booleanTypeSymbol; | ||
break; | ||
return types_1.boolean.refine(value => value === literal); | ||
default: | ||
throw new TypeError('Unsupported literal value'); | ||
} | ||
return new type_1.AtomicType(symbol, [value => value === literal]); | ||
} | ||
@@ -27,0 +22,0 @@ exports.literal = literal; |
{ | ||
"name": "x-value", | ||
"version": "0.0.1-11", | ||
"version": "0.0.1-12", | ||
"repository": "https://github.com/vilic/x-value.git", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -60,4 +60,7 @@ [![NPM version](https://img.shields.io/npm/v/x-value?color=%23cb3837&style=flat-square)](https://www.npmjs.com/package/x-value) | ||
// Or with nominal type. | ||
const Email = x.string.refine<'email'>(value => value.includes('@')); | ||
// Or with refined or nominal type. | ||
const Email = x.string.refine<`${string}@${string}`>(value => | ||
value.includes('@'), | ||
); | ||
const Email = x.string.refine<Nominal<'email'>>(value => value.includes('@')); | ||
``` | ||
@@ -64,0 +67,0 @@ |
@@ -32,4 +32,4 @@ import type { | ||
? __TupleMediumType<TTuple, TMediumTypes> | ||
: TType extends RefinedType<infer TType, infer TNominal> | ||
? __RefinedMediumType<TType, TNominal, TMediumTypes> | ||
: TType extends RefinedType<infer TType, infer TRefined, infer TNominal> | ||
? __RefinedMediumType<TType, TRefined, TNominal, TMediumTypes> | ||
: TType extends AtomicType<infer TTypeSymbol> | ||
@@ -74,13 +74,11 @@ ? __AtomicMediumType<TTypeSymbol, TMediumTypes> | ||
export type __RefinedMediumType<TType, TNominal, TMediumTypes> = __MediumTypeOf< | ||
TType, | ||
TMediumTypes | ||
> extends infer T | ||
? unknown extends TNominal | ||
? T | ||
: T & | ||
TNominal & { | ||
[TNominalTypeSymbol in typeof __nominalType]: Denominalize<T>; | ||
} | ||
: never; | ||
export type __RefinedMediumType<TType, TRefined, TNominal, TMediumTypes> = | ||
__MediumTypeOf<TType, TMediumTypes> & TRefined extends infer T | ||
? unknown extends TNominal | ||
? T | ||
: T & | ||
TNominal & { | ||
[TNominalTypeSymbol in typeof __nominalType]: Denominalize<T>; | ||
} | ||
: never; | ||
@@ -129,2 +127,16 @@ export type __AtomicMediumType< | ||
export type __RefinedType< | ||
TType extends Type, | ||
TNominalOrRefined, | ||
TNominal, | ||
> = RefinedType< | ||
TType, | ||
TNominalOrRefined extends __NominalPartial ? unknown : TNominalOrRefined, | ||
TNominalOrRefined extends __NominalPartial ? TNominalOrRefined : TNominal | ||
>; | ||
export type __NominalPartial = { | ||
[TNominalTypeSymbol in typeof __nominalType]: unknown; | ||
}; | ||
export function merge(partials: unknown[]): unknown { | ||
@@ -131,0 +143,0 @@ let pendingMergeKeyToValues: Map<string | number, unknown[]> | undefined; |
import {toString} from '../@utils'; | ||
import type {MediumAtomicCodecs} from '../medium'; | ||
import {dateTypeSymbol, regexpTypeSymbol} from '../types'; | ||
import {bigintTypeSymbol, dateTypeSymbol, regexpTypeSymbol} from '../types'; | ||
@@ -8,2 +8,3 @@ const REGEXP_LITERAL_REGEX = /^\/(.*)\/([^/]*)$/; | ||
export interface ExtendedTypes { | ||
[bigintTypeSymbol]: string; | ||
[dateTypeSymbol]: string; | ||
@@ -14,2 +15,16 @@ [regexpTypeSymbol]: string; | ||
export const EXTENDED_CODECS: MediumAtomicCodecs<ExtendedTypes> = { | ||
[bigintTypeSymbol]: { | ||
encode(bigint) { | ||
return bigint.toString(); | ||
}, | ||
decode(value) { | ||
if (typeof value !== 'string') { | ||
throw new TypeError( | ||
`Expected bigint string, getting ${toString.call(value)}`, | ||
); | ||
} | ||
return BigInt(value); | ||
}, | ||
}, | ||
[dateTypeSymbol]: { | ||
@@ -16,0 +31,0 @@ encode(date) { |
import {atomicTypeSymbol, medium} from '../medium'; | ||
import type { | ||
bigintTypeSymbol, | ||
booleanTypeSymbol, | ||
@@ -7,2 +8,3 @@ dateTypeSymbol, | ||
numberTypeSymbol, | ||
regexpTypeSymbol, | ||
stringTypeSymbol, | ||
@@ -21,4 +23,6 @@ undefinedTypeSymbol, | ||
[numberTypeSymbol]: number; | ||
[bigintTypeSymbol]: bigint; | ||
[booleanTypeSymbol]: boolean; | ||
[dateTypeSymbol]: Date; | ||
[regexpTypeSymbol]: RegExp; | ||
} | ||
@@ -25,0 +29,0 @@ |
@@ -5,8 +5,7 @@ import type { | ||
__MediumTypesPackedType, | ||
__RefinedType, | ||
} from '../@utils'; | ||
import {toString} from '../@utils'; | ||
import type {Medium} from '../medium'; | ||
import type {Nominal} from '../utils'; | ||
import type {RefinedType} from './refined-type'; | ||
import type {TypeConstraint, TypeIssue, TypeOf, TypePath} from './type'; | ||
@@ -16,5 +15,5 @@ import {Type} from './type'; | ||
export interface ArrayType<TElement> { | ||
refine<TNominal>( | ||
refine<TNominalOrRefined, TNominal = unknown>( | ||
constraints: __ElementOrArray<TypeConstraint<TypeOf<TElement>[]>>, | ||
): RefinedType<this, Nominal<TNominal>>; | ||
): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
@@ -21,0 +20,0 @@ decode<TMediumTypes extends object>( |
@@ -5,7 +5,6 @@ import type { | ||
__MediumTypesPackedType, | ||
__RefinedType, | ||
} from '../@utils'; | ||
import type {Medium} from '../medium'; | ||
import type {Nominal} from '../utils'; | ||
import type {RefinedType} from './refined-type'; | ||
import type {TypeConstraint, TypeIssue, TypePath} from './type'; | ||
@@ -24,7 +23,7 @@ import {Type} from './type'; | ||
export interface AtomicType<TSymbol> { | ||
refine<TNominal>( | ||
refine<TNominalOrRefined, TNominal = unknown>( | ||
constraints: __ElementOrArray< | ||
TypeConstraint<__AtomicMediumType<TSymbol, XValue.Types>> | ||
>, | ||
): RefinedType<this, Nominal<TNominal>>; | ||
): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
@@ -31,0 +30,0 @@ decode<TMediumTypes extends object>( |
@@ -5,2 +5,3 @@ import type { | ||
__MediumTypesPackedType, | ||
__RefinedType, | ||
__UnionToIntersection, | ||
@@ -10,5 +11,3 @@ } from '../@utils'; | ||
import type {Medium} from '../medium'; | ||
import type {Nominal} from '../utils'; | ||
import type {RefinedType} from './refined-type'; | ||
import type {TypeConstraint, TypeIssue, TypeOf, TypePath} from './type'; | ||
@@ -18,7 +17,7 @@ import {Type} from './type'; | ||
export interface IntersectionType<TTypeTuple> { | ||
refine<TNominal>( | ||
refine<TNominalOrRefined, TNominal = unknown>( | ||
constraints: __ElementOrArray< | ||
TypeConstraint<__UnionToIntersection<TypeOf<TTypeTuple[number]>>> | ||
>, | ||
): RefinedType<this, Nominal<TNominal>>; | ||
): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
@@ -25,0 +24,0 @@ decode<TMediumTypes extends object>( |
@@ -5,8 +5,7 @@ import type { | ||
__ObjectTypeDefinitionToMediumType, | ||
__RefinedType, | ||
} from '../@utils'; | ||
import {toString} from '../@utils'; | ||
import type {Medium} from '../medium'; | ||
import type {Nominal} from '../utils'; | ||
import type {RefinedType} from './refined-type'; | ||
import type {TypeConstraint, TypeIssue, TypePath} from './type'; | ||
@@ -16,3 +15,3 @@ import {Type} from './type'; | ||
export interface ObjectType<TTypeDefinition> { | ||
refine<TNominal>( | ||
refine<TNominalOrRefined, TNominal = unknown>( | ||
constraints: __ElementOrArray< | ||
@@ -23,3 +22,3 @@ TypeConstraint< | ||
>, | ||
): RefinedType<this, Nominal<TNominal>>; | ||
): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
@@ -26,0 +25,0 @@ decode<TMediumTypes extends object>( |
@@ -5,7 +5,6 @@ import type { | ||
__MediumTypesPackedType, | ||
__RefinedType, | ||
} from '../@utils'; | ||
import type {Medium} from '../medium'; | ||
import type {Nominal} from '../utils'; | ||
import type {RefinedType} from './refined-type'; | ||
import type {TypeConstraint, TypeIssue, TypeOf, TypePath} from './type'; | ||
@@ -15,5 +14,5 @@ import {Type} from './type'; | ||
export interface OptionalType<TType> { | ||
refine<TNominal>( | ||
refine<TNominalOrRefined, TNominal = unknown>( | ||
constraints: __ElementOrArray<TypeConstraint<TypeOf<TType> | undefined>>, | ||
): RefinedType<this, Nominal<TNominal>>; | ||
): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
@@ -20,0 +19,0 @@ decode<TMediumTypes extends object>( |
@@ -6,2 +6,3 @@ import type { | ||
__MediumTypesPackedType, | ||
__RefinedType, | ||
__TypeOfRecordKeyType, | ||
@@ -11,5 +12,3 @@ } from '../@utils'; | ||
import type {Medium} from '../medium'; | ||
import type {Nominal} from '../utils'; | ||
import type {RefinedType} from './refined-type'; | ||
import type {TypeConstraint, TypeIssue, TypeOf, TypePath} from './type'; | ||
@@ -19,7 +18,7 @@ import {Type} from './type'; | ||
export interface RecordType<TKey, TValue> { | ||
refine<TNominal>( | ||
refine<TNominalOrRefined, TNominal = unknown>( | ||
constraints: __ElementOrArray< | ||
TypeConstraint<Record<__TypeOfRecordKeyType<TKey>, TypeOf<TValue>>> | ||
>, | ||
): RefinedType<this, Nominal<TNominal>>; | ||
): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
@@ -26,0 +25,0 @@ decode<TMediumTypes extends object>( |
@@ -6,5 +6,6 @@ import type { | ||
__RefinedMediumType, | ||
__RefinedType, | ||
} from '../@utils'; | ||
import type {Medium} from '../medium'; | ||
import type {Nominal} from '../utils'; | ||
import type {__nominalType} from '../utils'; | ||
@@ -14,8 +15,10 @@ import type {TypeConstraint, TypeIssue, TypePath} from './type'; | ||
export interface RefinedType<TType, TNominal> { | ||
refine<TNominal>( | ||
export interface RefinedType<TType, TRefined, TNominal> { | ||
refine<TNominalOrRefined, TNominal = unknown>( | ||
constraints: __ElementOrArray< | ||
TypeConstraint<__RefinedMediumType<TType, TNominal, XValue.Types>> | ||
TypeConstraint< | ||
__RefinedMediumType<TType, TNominalOrRefined, TNominal, XValue.Types> | ||
> | ||
>, | ||
): RefinedType<this, Nominal<TNominal>>; | ||
): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
@@ -28,7 +31,7 @@ decode<TMediumTypes extends object>( | ||
>, | ||
): __RefinedMediumType<TType, TNominal, XValue.Types>; | ||
): __RefinedMediumType<TType, TRefined, TNominal, XValue.Types>; | ||
encode<TMediumTypes extends object>( | ||
medium: Medium<TMediumTypes>, | ||
value: __RefinedMediumType<TType, TNominal, XValue.Types>, | ||
value: __RefinedMediumType<TType, TRefined, TNominal, XValue.Types>, | ||
): __MediumTypesPackedType< | ||
@@ -53,6 +56,11 @@ TMediumTypes, | ||
value: unknown, | ||
): value is __RefinedMediumType<TType, TNominal, XValue.Types>; | ||
): value is __RefinedMediumType<TType, TRefined, TNominal, XValue.Types>; | ||
} | ||
export class RefinedType<TType extends Type, TNominal> extends Type<'refined'> { | ||
export class RefinedType< | ||
TType extends Type, | ||
TRefined, | ||
TNominal, | ||
> extends Type<'refined'> { | ||
protected __static_type_refined!: TRefined; | ||
protected __static_type_nominal!: TNominal; | ||
@@ -145,11 +153,1 @@ | ||
} | ||
export function refined<TType extends Type, TNominal>( | ||
Type: TType, | ||
constraints: TypeConstraint | TypeConstraint[], | ||
): RefinedType<TType, TNominal> { | ||
return new RefinedType( | ||
Type, | ||
Array.isArray(constraints) ? constraints : [constraints], | ||
); | ||
} |
import type { | ||
__ElementOrArray, | ||
__MediumTypesPackedType, | ||
__RefinedType, | ||
__TupleMediumType, | ||
@@ -8,5 +9,3 @@ } from '../@utils'; | ||
import type {Medium} from '../medium'; | ||
import type {Nominal} from '../utils'; | ||
import type {RefinedType} from './refined-type'; | ||
import type {TypeConstraint, TypeIssue, TypePath} from './type'; | ||
@@ -16,7 +15,7 @@ import {Type} from './type'; | ||
export interface TupleType<TElements> { | ||
refine<TNominal>( | ||
refine<TNominalOrRefined, TNominal = unknown>( | ||
constraints: __ElementOrArray< | ||
TypeConstraint<__TupleMediumType<TElements, XValue.Types>> | ||
>, | ||
): RefinedType<this, Nominal<TNominal>>; | ||
): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
@@ -23,0 +22,0 @@ decode<TMediumTypes extends object>( |
@@ -12,4 +12,7 @@ import type {__MediumTypeOf} from '../@utils'; | ||
constraints: TypeConstraint | TypeConstraint[], | ||
): RefinedType<Type, Nominal<unknown>> { | ||
return refined(this, constraints); | ||
): RefinedType<Type, unknown, Nominal<unknown>> { | ||
return new RefinedType( | ||
this, | ||
Array.isArray(constraints) ? constraints : [constraints], | ||
); | ||
} | ||
@@ -143,3 +146,2 @@ | ||
// Make sure code in refined-type.ts accessing type.ts after exports ready. | ||
import type {RefinedType} from './refined-type'; | ||
import {refined} from './refined-type'; | ||
import {RefinedType} from './refined-type'; |
@@ -5,7 +5,6 @@ import type { | ||
__MediumTypesPackedType, | ||
__RefinedType, | ||
} from '../@utils'; | ||
import type {Medium} from '../medium'; | ||
import type {Nominal} from '../utils'; | ||
import type {RefinedType} from './refined-type'; | ||
import type {TypeConstraint, TypeIssue, TypeOf, TypePath} from './type'; | ||
@@ -15,5 +14,5 @@ import {Type} from './type'; | ||
export interface UnionType<TTypeTuple> { | ||
refine<TNominal>( | ||
refine<TNominalOrRefined, TNominal = unknown>( | ||
constraints: __ElementOrArray<TypeConstraint<TypeOf<TTypeTuple[number]>>>, | ||
): RefinedType<this, Nominal<TNominal>>; | ||
): __RefinedType<this, TNominalOrRefined, TNominal>; | ||
@@ -20,0 +19,0 @@ decode<TMediumTypes extends object>( |
@@ -9,5 +9,7 @@ import {toString} from './@utils'; | ||
[undefinedTypeSymbol]: undefined; | ||
[voidTypeSymbol]: void; | ||
[nullTypeSymbol]: null; | ||
[stringTypeSymbol]: string; | ||
[numberTypeSymbol]: number; | ||
[bigintTypeSymbol]: bigint; | ||
[booleanTypeSymbol]: boolean; | ||
@@ -56,2 +58,9 @@ [dateTypeSymbol]: Date; | ||
export const bigintTypeSymbol = Symbol(); | ||
export const bigint = atomic(bigintTypeSymbol, value => | ||
typeof value === 'bigint' | ||
? true | ||
: `Expected bigint, getting ${toString.call(value)}.`, | ||
); | ||
export const booleanTypeSymbol = Symbol(); | ||
@@ -58,0 +67,0 @@ export const boolean = atomic(booleanTypeSymbol, value => |
import isEqual from 'lodash.isequal'; | ||
import type {Type, TypeOf} from './type'; | ||
import {AtomicType, RefinedType, record} from './type'; | ||
import { | ||
booleanTypeSymbol, | ||
numberTypeSymbol, | ||
string, | ||
stringTypeSymbol, | ||
unknown, | ||
} from './types'; | ||
import {RefinedType, record} from './type'; | ||
import {boolean, number, string, unknown} from './types'; | ||
@@ -29,14 +23,11 @@ /** | ||
export type Nominal<TNominal, T = unknown> = T & | ||
(unknown extends TNominal | ||
? unknown | ||
: { | ||
[TNominalTypeSymbol in typeof __nominalType]: T; | ||
} & ([TNominal] extends [string | symbol] | ||
? { | ||
[TNominalSymbol in typeof __nominal]: { | ||
[TNominalKey in TNominal]: true; | ||
}; | ||
} | ||
: TNominal)); | ||
export type Nominal<TNominal, T = unknown> = T & { | ||
[TNominalTypeSymbol in typeof __nominalType]: T; | ||
} & ([TNominal] extends [string | symbol] | ||
? { | ||
[TNominalSymbol in typeof __nominal]: { | ||
[TNominalKey in TNominal]: true; | ||
}; | ||
} | ||
: TNominal); | ||
@@ -53,41 +44,34 @@ export type Denominalize<T> = T extends { | ||
literal: T, | ||
): AtomicType<typeof stringTypeSymbol>; | ||
): RefinedType<typeof string, T, unknown>; | ||
export function literal<T extends number>( | ||
literal: T, | ||
): AtomicType<typeof numberTypeSymbol>; | ||
): RefinedType<typeof number, T, unknown>; | ||
export function literal<T extends boolean>( | ||
literal: T, | ||
): AtomicType<typeof booleanTypeSymbol>; | ||
export function literal(literal: unknown): AtomicType<any> { | ||
let symbol: symbol; | ||
): RefinedType<typeof boolean, T, unknown>; | ||
export function literal(literal: unknown): RefinedType<Type, unknown, unknown> { | ||
switch (typeof literal) { | ||
case 'string': | ||
symbol = stringTypeSymbol; | ||
break; | ||
return string.refine(value => value === literal); | ||
case 'number': | ||
symbol = numberTypeSymbol; | ||
break; | ||
return number.refine(value => value === literal); | ||
case 'boolean': | ||
symbol = booleanTypeSymbol; | ||
break; | ||
return boolean.refine(value => value === literal); | ||
default: | ||
throw new TypeError('Unsupported literal value'); | ||
} | ||
return new AtomicType(symbol, [value => value === literal]); | ||
} | ||
export function equal( | ||
comparison: unknown, | ||
): RefinedType<typeof unknown, unknown>; | ||
export function equal<TType extends Type>( | ||
comparison: TypeOf<TType>, | ||
export function equal<T>( | ||
comparison: T, | ||
): RefinedType<typeof unknown, T, unknown>; | ||
export function equal<T extends TypeOf<TType>, TType extends Type>( | ||
comparison: T, | ||
Type: TType, | ||
): RefinedType<TType, unknown>; | ||
): RefinedType<TType, T, unknown>; | ||
export function equal( | ||
comparison: unknown, | ||
Type = unknown, | ||
): RefinedType<Type, unknown> { | ||
): RefinedType<Type, unknown, unknown> { | ||
return new RefinedType(Type, [value => isEqual(value, comparison)]); | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
182176
3879
239