Socket
Socket
Sign inDemoInstall

io-ts

Package Overview
Dependencies
Maintainers
1
Versions
120
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

io-ts - npm Package Compare versions

Comparing version 1.8.0 to 1.8.1

4

CHANGELOG.md

@@ -17,6 +17,6 @@ # Changelog

# 1.8.0
# 1.8.1
- **New Feature**
- add `brand` combinator (@gcanti)
- add `brand` combinator (@gcanti, @lostintime)
- add `Int` codec (@gcanti)

@@ -23,0 +23,0 @@ - `exact` strips additional properties while decoding / encoding (@gcanti)

import { Either } from 'fp-ts/lib/Either';
import { Predicate } from 'fp-ts/lib/function';
import { Predicate, Refinement } from 'fp-ts/lib/function';
/**

@@ -137,13 +137,3 @@ * @since 1.0.0

* @since 1.0.0
* @deprecated
*/
export declare const getValidationError: (value: unknown, context: Context) => ValidationError;
/**
* @since 1.0.0
* @deprecated
*/
export declare const getDefaultContext: (decoder: Decoder<any, any>) => Context;
/**
* @since 1.0.0
*/
export declare const appendContext: (c: Context, key: string, decoder: Decoder<any, any>, actual?: unknown) => Context;

@@ -210,20 +200,2 @@ /**

/**
* @since 1.0.0
*/
export declare class AnyType extends Type<any> {
readonly _tag: 'AnyType';
constructor();
}
/**
* @since 1.5.3
*/
export interface AnyC extends AnyType {
}
/**
* Use `unknown` instead
* @since 1.0.0
* @deprecated
*/
export declare const any: AnyC;
/**
* @since 1.5.0

@@ -343,28 +315,4 @@ */

/**
* Use `UnknownRecord` instead
* @since 1.0.0
* @deprecated
*/
export declare const Dictionary: UnknownRecordC;
/**
* @since 1.0.0
*/
export declare class ObjectType extends Type<object> {
readonly _tag: 'ObjectType';
constructor();
}
/**
* @since 1.5.3
*/
export interface ObjectC extends ObjectType {
}
/**
* Use `UnknownRecord` instead
* @since 1.0.0
* @deprecated
*/
export declare const object: ObjectC;
/**
* @since 1.0.0
*/
export declare class FunctionType extends Type<Function> {

@@ -392,41 +340,37 @@ readonly _tag: 'FunctionType';

}
declare const _brand: unique symbol;
/**
* Use `BrandC` instead
* @since 1.5.3
* @deprecated
* @since 1.8.1
*/
export interface RefinementC<C extends Any> extends RefinementType<C, TypeOf<C>, OutputOf<C>, InputOf<C>> {
export interface Brand<B> {
readonly [_brand]: B;
}
/**
* Use `brand` instead
* @since 1.0.0
* @deprecated
* @since 1.8.1
*/
export declare const refinement: <C extends Any>(codec: C, predicate: Predicate<C["_A"]>, name?: string) => RefinementC<C>;
export declare type Branded<A, B> = A & Brand<B>;
/**
* Use `Int` instead
* @since 1.0.0
* @deprecated
* @since 1.8.1
*/
export declare const Integer: RefinementC<NumberC>;
declare const _brand: unique symbol;
export interface BrandC<C extends Any, B> extends RefinementType<C, Branded<TypeOf<C>, B>, OutputOf<C>, InputOf<C>> {
}
/**
* @since 1.8.0
* @since 1.8.1
*/
export interface Brand<B> {
readonly [_brand]: B;
}
export declare const brand: <C extends Any, N extends string, B extends { readonly [K in N]: symbol; }>(codec: C, predicate: Refinement<C["_A"], Branded<C["_A"], B>>, name: N) => BrandC<C, B>;
/**
* @since 1.8.0
* @since 1.8.1
*/
export interface BrandC<C extends Any, B> extends RefinementType<C, TypeOf<C> & Brand<B>, OutputOf<C>, InputOf<C>> {
export interface IntBrand {
readonly Int: unique symbol;
}
/**
* @since 1.8.0
* A branded codec representing an integer
* @since 1.8.1
*/
export declare const brand: <C extends Any, B extends string>(codec: C, predicate: Predicate<C["_A"]>, name: B) => BrandC<C, B>;
export declare const Int: BrandC<NumberC, IntBrand>;
/**
* @since 1.8.0
* @since 1.8.1
*/
export declare const Int: BrandC<NumberC, "Int">;
export declare type Int = Branded<number, IntBrand>;
declare type LiteralValue = string | number | boolean;

@@ -619,10 +563,4 @@ /**

/**
* Use `record` instead
* @since 1.0.0
* @deprecated
*/
export declare const dictionary: typeof record;
/**
* @since 1.0.0
*/
export declare class UnionType<CS extends Array<Any>, A = any, O = A, I = unknown> extends Type<A, O, I> {

@@ -651,10 +589,2 @@ readonly types: CS;

/**
* used in `intersection` as a workaround for #234
* @since 1.4.2
* @deprecated
*/
export declare type Compact<A> = {
[K in keyof A]: A[K];
};
/**
* @since 1.5.3

@@ -731,21 +661,2 @@ */

/**
* @since 1.0.0
* @deprecated
*/
export declare class StrictType<P, A = any, O = A, I = unknown> extends Type<A, O, I> {
readonly props: P;
readonly _tag: 'StrictType';
constructor(name: string, is: StrictType<P, A, O, I>['is'], validate: StrictType<P, A, O, I>['validate'], encode: StrictType<P, A, O, I>['encode'], props: P);
}
/**
* @since 1.5.3
* @deprecated
*/
export interface StrictC<P extends Props> extends StrictType<P, {
[K in keyof P]: TypeOf<P[K]>;
}, {
[K in keyof P]: OutputOf<P[K]>;
}, unknown> {
}
/**
* Strips additional properties

@@ -755,36 +666,2 @@ * @since 1.0.0

export declare const strict: <P extends Props>(props: P, name?: string | undefined) => ExactC<TypeC<P>>;
/**
* @since 1.3.0
*/
export declare type TaggedProps<Tag extends string> = {
[K in Tag]: LiteralType<any>;
};
/**
* @since 1.3.0
*/
export interface TaggedRefinement<Tag extends string, A, O = A> extends RefinementType<Tagged<Tag>, A, O> {
}
/**
* @since 1.3.0
*/
export interface TaggedUnion<Tag extends string, A, O = A> extends UnionType<Array<Tagged<Tag>>, A, O> {
}
/**
* @since 1.3.0
*/
export declare type TaggedIntersectionArgument<Tag extends string> = [Tagged<Tag>] | [Tagged<Tag>, Mixed] | [Mixed, Tagged<Tag>] | [Tagged<Tag>, Mixed, Mixed] | [Mixed, Tagged<Tag>, Mixed] | [Mixed, Mixed, Tagged<Tag>] | [Tagged<Tag>, Mixed, Mixed, Mixed] | [Mixed, Tagged<Tag>, Mixed, Mixed] | [Mixed, Mixed, Tagged<Tag>, Mixed] | [Mixed, Mixed, Mixed, Tagged<Tag>] | [Tagged<Tag>, Mixed, Mixed, Mixed, Mixed] | [Mixed, Tagged<Tag>, Mixed, Mixed, Mixed] | [Mixed, Mixed, Tagged<Tag>, Mixed, Mixed] | [Mixed, Mixed, Mixed, Tagged<Tag>, Mixed] | [Mixed, Mixed, Mixed, Mixed, Tagged<Tag>];
/**
* @since 1.3.0
*/
export interface TaggedIntersection<Tag extends string, A, O = A> extends IntersectionType<TaggedIntersectionArgument<Tag>, A, O> {
}
/**
* @since 1.3.0
*/
export interface TaggedExact<Tag extends string, A, O = A> extends ExactType<Tagged<Tag>, A, O> {
}
/**
* @since 1.3.0
*/
export declare type Tagged<Tag extends string, A = any, O = A> = InterfaceType<TaggedProps<Tag>, A, O> | StrictType<TaggedProps<Tag>, A, O> | TaggedRefinement<Tag, A, O> | TaggedUnion<Tag, A, O> | TaggedIntersection<Tag, A, O> | TaggedExact<Tag, A, O> | RecursiveType<any, A, O>;
declare type IndexItem = [unknown, Any, Any];

@@ -804,3 +681,3 @@ interface Index extends Array<IndexItem> {

*/
export declare class TaggedUnionType<Tag extends string, CS extends Array<Tagged<Tag>>, A = any, O = A, I = unknown> extends UnionType<CS, A, O, I> {
export declare class TaggedUnionType<Tag extends string, CS extends Array<Mixed>, A = any, O = A, I = unknown> extends UnionType<CS, A, O, I> {
readonly tag: Tag;

@@ -812,3 +689,3 @@ constructor(name: string, is: TaggedUnionType<Tag, CS, A, O, I>['is'], validate: TaggedUnionType<Tag, CS, A, O, I>['validate'], encode: TaggedUnionType<Tag, CS, A, O, I>['encode'], codecs: CS, tag: Tag);

*/
export interface TaggedUnionC<Tag extends string, CS extends [Tagged<Tag>, Tagged<Tag>, ...Array<Tagged<Tag>>]> extends TaggedUnionType<Tag, CS, TypeOf<CS[number]>, OutputOf<CS[number]>, unknown> {
export interface TaggedUnionC<Tag extends string, CS extends [Mixed, Mixed, ...Array<Mixed>]> extends TaggedUnionType<Tag, CS, TypeOf<CS[number]>, OutputOf<CS[number]>, unknown> {
}

@@ -818,3 +695,3 @@ /**

*/
export declare const taggedUnion: <Tag extends string, CS extends [Tagged<Tag, any, any>, Tagged<Tag, any, any>, ...Tagged<Tag, any, any>[]]>(tag: Tag, codecs: CS, name?: string) => TaggedUnionC<Tag, CS>;
export declare const taggedUnion: <Tag extends string, CS extends [Mixed, Mixed, ...Mixed[]]>(tag: Tag, codecs: CS, name?: string) => TaggedUnionC<Tag, CS>;
/**

@@ -857,3 +734,153 @@ * @since 1.1.0

export declare const exact: <C extends HasProps>(codec: C, name?: string) => ExactC<C>;
export { nullType as null, undefinedType as undefined, UnknownArray as Array, type as interface, voidType as void };
/**
* @since 1.0.0
* @deprecated
*/
export declare const getValidationError: (value: unknown, context: Context) => ValidationError;
/**
* @since 1.0.0
* @deprecated
*/
export declare const getDefaultContext: (decoder: Decoder<any, any>) => Context;
/**
* @since 1.0.0
* @deprecated
*/
export declare class AnyType extends Type<any> {
readonly _tag: 'AnyType';
constructor();
}
/**
* @since 1.5.3
* @deprecated
*/
export interface AnyC extends AnyType {
}
/**
* Use `unknown` instead
* @since 1.0.0
* @deprecated
*/
export declare const any: AnyC;
/**
* Use `UnknownRecord` instead
* @since 1.0.0
* @deprecated
*/
export declare const Dictionary: UnknownRecordC;
/**
* @since 1.0.0
* @deprecated
*/
export declare class ObjectType extends Type<object> {
readonly _tag: 'ObjectType';
constructor();
}
/**
* @since 1.5.3
* @deprecated
*/
export interface ObjectC extends ObjectType {
}
/**
* Use `UnknownRecord` instead
* @since 1.0.0
* @deprecated
*/
export declare const object: ObjectC;
/**
* Use `BrandC` instead
* @since 1.5.3
* @deprecated
*/
export interface RefinementC<C extends Any> extends RefinementType<C, TypeOf<C>, OutputOf<C>, InputOf<C>> {
}
/**
* Use `brand` instead
* @since 1.0.0
* @deprecated
*/
export declare function refinement<C extends Any>(codec: C, predicate: Predicate<TypeOf<C>>, name?: string): RefinementC<C>;
/**
* Use `Int` instead
* @since 1.0.0
* @deprecated
*/
export declare const Integer: RefinementC<NumberC>;
/**
* Use `record` instead
* @since 1.0.0
* @deprecated
*/
export declare const dictionary: typeof record;
/**
* used in `intersection` as a workaround for #234
* @since 1.4.2
* @deprecated
*/
export declare type Compact<A> = {
[K in keyof A]: A[K];
};
/**
* @since 1.0.0
* @deprecated
*/
export declare class StrictType<P, A = any, O = A, I = unknown> extends Type<A, O, I> {
readonly props: P;
readonly _tag: 'StrictType';
constructor(name: string, is: StrictType<P, A, O, I>['is'], validate: StrictType<P, A, O, I>['validate'], encode: StrictType<P, A, O, I>['encode'], props: P);
}
/**
* @since 1.5.3
* @deprecated
*/
export interface StrictC<P extends Props> extends StrictType<P, {
[K in keyof P]: TypeOf<P[K]>;
}, {
[K in keyof P]: OutputOf<P[K]>;
}, unknown> {
}
/**
* @since 1.3.0
* @deprecated
*/
export declare type TaggedProps<Tag extends string> = {
[K in Tag]: LiteralType<any>;
};
/**
* @since 1.3.0
* @deprecated
*/
export interface TaggedRefinement<Tag extends string, A, O = A> extends RefinementType<Tagged<Tag>, A, O> {
}
/**
* @since 1.3.0
* @deprecated
*/
export interface TaggedUnion<Tag extends string, A, O = A> extends UnionType<Array<Tagged<Tag>>, A, O> {
}
/**
* @since 1.3.0
* @deprecated
*/
export declare type TaggedIntersectionArgument<Tag extends string> = [Tagged<Tag>] | [Tagged<Tag>, Mixed] | [Mixed, Tagged<Tag>] | [Tagged<Tag>, Mixed, Mixed] | [Mixed, Tagged<Tag>, Mixed] | [Mixed, Mixed, Tagged<Tag>] | [Tagged<Tag>, Mixed, Mixed, Mixed] | [Mixed, Tagged<Tag>, Mixed, Mixed] | [Mixed, Mixed, Tagged<Tag>, Mixed] | [Mixed, Mixed, Mixed, Tagged<Tag>] | [Tagged<Tag>, Mixed, Mixed, Mixed, Mixed] | [Mixed, Tagged<Tag>, Mixed, Mixed, Mixed] | [Mixed, Mixed, Tagged<Tag>, Mixed, Mixed] | [Mixed, Mixed, Mixed, Tagged<Tag>, Mixed] | [Mixed, Mixed, Mixed, Mixed, Tagged<Tag>];
/**
* @since 1.3.0
* @deprecated
*/
export interface TaggedIntersection<Tag extends string, A, O = A> extends IntersectionType<TaggedIntersectionArgument<Tag>, A, O> {
}
/**
* @since 1.3.0
* @deprecated
*/
export interface TaggedExact<Tag extends string, A, O = A> extends ExactType<Tagged<Tag>, A, O> {
}
/**
* @since 1.3.0
* @deprecated
*/
export declare type Tagged<Tag extends string, A = any, O = A> = InterfaceType<TaggedProps<Tag>, A, O> | StrictType<TaggedProps<Tag>, A, O> | TaggedRefinement<Tag, A, O> | TaggedUnion<Tag, A, O> | TaggedIntersection<Tag, A, O> | TaggedExact<Tag, A, O> | RecursiveType<any, A, O>;
/**
* Drops the codec "kind"

@@ -892,2 +919,1 @@ * @since 1.1.0

export declare function alias<A, O, P, I>(codec: InterfaceType<P, A, O, I>): <AA extends Exact<A, AA>, OO extends Exact<O, OO> = O, PP extends Exact<P, PP> = P, II extends I = I>() => InterfaceType<PP, AA, OO, II>;
export { nullType as null, undefinedType as undefined, UnknownArray as Array, type as interface, voidType as void };

@@ -86,18 +86,3 @@ "use strict";

* @since 1.0.0
* @deprecated
*/
exports.getValidationError /* istanbul ignore next */ = function (value, context) { return ({
value: value,
context: context
}); };
/**
* @since 1.0.0
* @deprecated
*/
exports.getDefaultContext /* istanbul ignore next */ = function (decoder) { return [
{ key: '', type: decoder }
]; };
/**
* @since 1.0.0
*/
exports.appendContext = function (c, key, decoder, actual) {

@@ -201,21 +186,2 @@ var len = c.length;

/**
* @since 1.0.0
*/
var AnyType = /** @class */ (function (_super) {
__extends(AnyType, _super);
function AnyType() {
var _this = _super.call(this, 'any', function (_) { return true; }, exports.success, exports.identity) || this;
_this._tag = 'AnyType';
return _this;
}
return AnyType;
}(Type));
exports.AnyType = AnyType;
/**
* Use `unknown` instead
* @since 1.0.0
* @deprecated
*/
exports.any = new AnyType();
/**
* @since 1.5.0

@@ -345,29 +311,4 @@ */

/**
* Use `UnknownRecord` instead
* @since 1.0.0
* @deprecated
*/
exports.Dictionary = exports.UnknownRecord;
/**
* @since 1.0.0
*/
var ObjectType = /** @class */ (function (_super) {
__extends(ObjectType, _super);
function ObjectType() {
var _this = _super.call(this, 'object', exports.UnknownRecord.is, exports.UnknownRecord.validate, exports.identity) || this;
_this._tag = 'ObjectType';
return _this;
}
return ObjectType;
}(Type));
exports.ObjectType = ObjectType;
/**
* Use `UnknownRecord` instead
* @since 1.0.0
* @deprecated
*/
exports.object = new ObjectType();
/**
* @since 1.0.0
*/
var FunctionType = /** @class */ (function (_super) {

@@ -405,33 +346,12 @@ __extends(FunctionType, _super);

/**
* Use `brand` instead
* @since 1.0.0
* @deprecated
* @since 1.8.1
*/
exports.refinement = function (codec, predicate, name) {
if (name === void 0) { name = "(" + codec.name + " | " + exports.getFunctionName(predicate) + ")"; }
return new RefinementType(name, function (u) { return codec.is(u) && predicate(u); }, function (i, c) {
var validation = codec.validate(i, c);
if (validation.isLeft()) {
return validation;
}
var a = validation.value;
return predicate(a) ? exports.success(a) : exports.failure(a, c);
}, codec.encode, codec, predicate);
};
/**
* Use `Int` instead
* @since 1.0.0
* @deprecated
*/
exports.Integer = exports.refinement(exports.number, Number.isInteger, 'Integer');
/**
* @since 1.8.0
*/
exports.brand = function (codec, predicate, name) {
return exports.refinement(codec, predicate, name);
return refinement(codec, predicate, name);
};
/**
* @since 1.8.0
* A branded codec representing an integer
* @since 1.8.1
*/
exports.Int = exports.brand(exports.number, Number.isInteger, 'Int');
exports.Int = exports.brand(exports.number, function (n) { return Number.isInteger(n); }, 'Int');
/**

@@ -834,10 +754,4 @@ * @since 1.0.0

/**
* Use `record` instead
* @since 1.0.0
* @deprecated
*/
exports.dictionary = exports.record;
/**
* @since 1.0.0
*/
var UnionType = /** @class */ (function (_super) {

@@ -900,8 +814,17 @@ __extends(UnionType, _super);

exports.IntersectionType = IntersectionType;
var mergeAll = function (us) {
var r = us[0];
for (var i = 1; i < us.length; i++) {
var mergeAll = function (base, us) {
var r = base;
for (var i = 0; i < us.length; i++) {
var u = us[i];
if (u !== r) {
r = Object.assign(r, u);
if (u !== base) {
// `u` contains a prismatic value or is the result of a stripping combinator
if (r === base) {
r = Object.assign({}, u);
continue;
}
for (var k in u) {
if (u[k] !== base[k] || !r.hasOwnProperty(k)) {
r[k] = u[k];
}
}
}

@@ -929,4 +852,4 @@ }

}
return errors.length > 0 ? exports.failures(errors) : exports.success(mergeAll(us));
}, codecs.length === 0 ? exports.identity : function (a) { return mergeAll(codecs.map(function (codec) { return codec.encode(a); })); }, codecs);
return errors.length > 0 ? exports.failures(errors) : exports.success(mergeAll(u, us));
}, codecs.length === 0 ? exports.identity : function (a) { return mergeAll(a, codecs.map(function (codec) { return codec.encode(a); })); }, codecs);
}

@@ -1039,17 +962,2 @@ exports.intersection = intersection;

/**
* @since 1.0.0
* @deprecated
*/
var StrictType = /** @class */ (function (_super) {
__extends(StrictType, _super);
function StrictType(name, is, validate, encode, props) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.props = props;
_this._tag = 'StrictType';
return _this;
}
return StrictType;
}(Type));
exports.StrictType = StrictType;
/**
* Strips additional properties

@@ -1256,3 +1164,3 @@ * @since 1.0.0

if (isRecursiveCodecIndexable && codecs.length > 0) {
console.warn("[io-ts] Cannot build a tagged union for (B | A), returning a de-optimized union");
console.warn("[io-ts] Cannot build a tagged union for " + name + ", returning a de-optimized union");
}

@@ -1334,3 +1242,111 @@ var U = exports.union(codecs, name);

};
//
// deprecations
//
/**
* @since 1.0.0
* @deprecated
*/
exports.getValidationError /* istanbul ignore next */ = function (value, context) { return ({
value: value,
context: context
}); };
/**
* @since 1.0.0
* @deprecated
*/
exports.getDefaultContext /* istanbul ignore next */ = function (decoder) { return [
{ key: '', type: decoder }
]; };
/**
* @since 1.0.0
* @deprecated
*/
var AnyType = /** @class */ (function (_super) {
__extends(AnyType, _super);
function AnyType() {
var _this = _super.call(this, 'any', function (_) { return true; }, exports.success, exports.identity) || this;
_this._tag = 'AnyType';
return _this;
}
return AnyType;
}(Type));
exports.AnyType = AnyType;
/**
* Use `unknown` instead
* @since 1.0.0
* @deprecated
*/
exports.any = new AnyType();
/**
* Use `UnknownRecord` instead
* @since 1.0.0
* @deprecated
*/
exports.Dictionary = exports.UnknownRecord;
/**
* @since 1.0.0
* @deprecated
*/
var ObjectType = /** @class */ (function (_super) {
__extends(ObjectType, _super);
function ObjectType() {
var _this = _super.call(this, 'object', exports.UnknownRecord.is, exports.UnknownRecord.validate, exports.identity) || this;
_this._tag = 'ObjectType';
return _this;
}
return ObjectType;
}(Type));
exports.ObjectType = ObjectType;
/**
* Use `UnknownRecord` instead
* @since 1.0.0
* @deprecated
*/
exports.object = new ObjectType();
/**
* Use `brand` instead
* @since 1.0.0
* @deprecated
*/
function refinement(codec, predicate, name) {
if (name === void 0) { name = "(" + codec.name + " | " + exports.getFunctionName(predicate) + ")"; }
return new RefinementType(name, function (u) { return codec.is(u) && predicate(u); }, function (i, c) {
var validation = codec.validate(i, c);
if (validation.isLeft()) {
return validation;
}
var a = validation.value;
return predicate(a) ? exports.success(a) : exports.failure(a, c);
}, codec.encode, codec, predicate);
}
exports.refinement = refinement;
/**
* Use `Int` instead
* @since 1.0.0
* @deprecated
*/
exports.Integer = refinement(exports.number, Number.isInteger, 'Integer');
/**
* Use `record` instead
* @since 1.0.0
* @deprecated
*/
exports.dictionary = exports.record;
/**
* @since 1.0.0
* @deprecated
*/
var StrictType = /** @class */ (function (_super) {
__extends(StrictType, _super);
function StrictType(name, is, validate, encode, props) {
var _this = _super.call(this, name, is, validate, encode) || this;
_this.props = props;
_this._tag = 'StrictType';
return _this;
}
return StrictType;
}(Type));
exports.StrictType = StrictType;
/**
* Drops the codec "kind"

@@ -1337,0 +1353,0 @@ * @since 1.1.0

{
"name": "io-ts",
"version": "1.8.0",
"version": "1.8.1",
"description": "TypeScript compatible runtime type system for IO validation",

@@ -5,0 +5,0 @@ "files": [

@@ -309,13 +309,27 @@ [![build status](https://img.shields.io/travis/gcanti/io-ts/master.svg?style=flat-square)](https://travis-ci.org/gcanti/io-ts)

You can refine a codec (_any_ codec) using the `brand` combinator
You can brand / refine a codec (_any_ codec) using the `brand` combinator
```ts
const Positive = t.brand(t.number, n => n >= 0, 'Positive')
// a unique brand for positive numbers
interface PositiveBrand {
readonly Positive: unique symbol // use `unique symbol` here to ensure uniqueness across modules / packages
}
const Positive = t.brand(
t.number, // a codec representing the type to be refined
(n): n is t.Branded<number, PositiveBrand> => n >= 0, // a custom type guard using the build-in helper `Branded`
'Positive' // the name must match the readonly field in the brand
)
type Positive = t.TypeOf<typeof Positive>
/*
same as
type Positive = number & t.Brand<"Positive">
type Positive = number & t.Brand<PositiveBrand>
*/
```
Branded codecs can be merged with `t.intersection`
```ts
// t.Int is a built-in branded codec
const PositiveInt = t.intersection([t.Int, Positive])

@@ -326,20 +340,4 @@

same as
type PositiveInt = number & t.Brand<"Int"> & t.Brand<"Positive">
type PositiveInt = number & t.Brand<t.IntBrand> & t.Brand<PositiveBrand>
*/
const Person = t.type({
name: t.string,
age: PositiveInt
})
const Adult = t.brand(Person, person => person.age >= 18, 'Adult')
type Adult = t.TypeOf<typeof Adult>
/*
same as
type Adult = {
name: string;
age: number & t.Brand<"Int"> & t.Brand<"Positive">;
} & t.Brand<"Adult">
*/
```

@@ -346,0 +344,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc