@tsgonest/types

API reference for the @tsgonest/types package — zero-runtime branded types for validation.

npm install @tsgonest/types

Zero-runtime branded types that the tsgonest compiler reads at build time to generate validation and serialization code. These types produce no runtime code — they exist purely for type safety and IDE autocomplete.

import { tags } from '@tsgonest/types';

interface CreateUserDto {
  email: string & tags.Email;
  name: string & tags.Trim & tags.Min<1> & tags.Max<255>;
  age: number & tags.Min<0> & tags.Max<150>;
}

All types are exported under the tags namespace.


String Formats

Format<F>

Validates a string against a well-known format.

email: string & tags.Format<"email">
id: string & tags.Format<"uuid">

With custom error:

email: string & tags.Format<{ type: "email", error: "Must be a valid email" }>

Supported formats:

CategoryFormats
Emailemail, idn-email
URL / URIurl, uri, uri-reference, uri-template, iri, iri-reference
Networkipv4, ipv6, hostname, idn-hostname, cidrv4, cidrv6, mac
Identityuuid, nanoid, cuid, cuid2, ulid, jwt
Date / Timedate-time, date, time, duration
Encodingbyte, base64url, hex, json-pointer, relative-json-pointer
Otherregex, password, emoji

Format aliases (shorthand for common formats):

AliasEquivalent
EmailFormat<"email">
UuidFormat<"uuid">
UrlFormat<"url">
UriFormat<"uri">
IPv4Format<"ipv4">
IPv6Format<"ipv6">
DateTimeFormat<"date-time">
DateOnlyFormat<"date">
TimeFormat<"time">
DurationFormat<"duration">
JwtFormat<"jwt">
UlidFormat<"ulid">
CuidFormat<"cuid">
Cuid2Format<"cuid2">
NanoIdFormat<"nanoid">

String Constraints

TypeDescriptionExample
MinLength<N>Minimum string lengthMinLength<1>
MaxLength<N>Maximum string lengthMaxLength<255>
Pattern<P>Regex patternPattern<"^[a-z]+$">
StartsWith<S>Must start with prefixStartsWith<"https://">
EndsWith<S>Must end with suffixEndsWith<".json">
Includes<S>Must contain substringIncludes<"@">
UppercaseMust be all uppercaseUppercase
LowercaseMust be all lowercaseLowercase

All accept an extended form with custom errors:

name: string & tags.MinLength<{ value: 1, error: "Name is required" }>

Numeric Constraints

TypeAliasDescriptionExample
Minimum<N>Min<N>, Gte<N>Inclusive minimum (>=)Min<0>
Maximum<N>Max<N>, Lte<N>Inclusive maximum (<=)Max<100>
ExclusiveMinimum<N>Gt<N>Exclusive minimum (>)Gt<0>
ExclusiveMaximum<N>Lt<N>Exclusive maximum (<)Lt<100>
MultipleOf<N>Step<N>Must be a multiple of NStep<0.01>

Numeric Type Constraints

TypeDescriptionRange
Type<"int32">32-bit signed integer-2,147,483,648 to 2,147,483,647
Type<"uint32">32-bit unsigned integer0 to 4,294,967,295
Type<"int64">Safe integer range-(2^53 - 1) to 2^53 - 1
Type<"uint64">Unsigned safe integer0 to 2^53 - 1
Type<"float">Finite floatNo Infinity or NaN
Type<"double">Double precisionNo Infinity or NaN

Numeric aliases:

AliasEquivalent
IntType<"int32">
UintType<"uint32">
SafeIntType<"int64">
DoubleType<"double">
FiniteType<"float">
PositiveExclusiveMinimum<0>
NegativeExclusiveMaximum<0>
NonNegativeMinimum<0>
NonPositiveMaximum<0>

Array Constraints

TypeAliasDescriptionExample
MinItems<N>Minimum array lengthMinItems<1>
MaxItems<N>Maximum array lengthMaxItems<100>
UniqueItemsUniqueAll items must be uniqueUniqueItems

Transforms

Applied before validation. Never fail.

TypeDescription
TrimTrim leading/trailing whitespace
ToLowerCaseConvert to lowercase
ToUpperCaseConvert to uppercase
email: string & tags.Trim & tags.ToLowerCase & tags.Email

Coercion

Coerce

Coerces string inputs to the declared type before validation. Useful for query parameters where everything arrives as a string.

interface FilterDto {
  page: number & tags.Coerce;     // "42" → 42
  active: boolean & tags.Coerce;  // "true" → true
}

Custom Validators

Validate<F>

Validate using a custom predicate function. tsgonest resolves the function's source file and emits an import in the generated validator.

import { isValidCard } from './validators/credit-card';

interface PaymentDto {
  card: string & tags.Validate<typeof isValidCard>;
}

// With custom error:
interface StrictPaymentDto {
  card: string & tags.Validate<{ fn: typeof isValidCard, error: "Invalid card number" }>;
}

Meta Types

Default<V>

Default value for optional properties. Assigned when value is undefined.

theme?: string & tags.Default<"light">
limit?: number & tags.Default<20>

Error<M>

Global error message for all validation failures on a property. Per-constraint errors take precedence.

email: string & tags.Email & tags.Error<"Invalid email address">

Compound Types

TypeDescriptionEquivalent
Length<N>Exact string lengthMinLength<N> & MaxLength<N>
Range<{min, max}>Numeric range (inclusive)Minimum<min> & Maximum<max>
Between<{min, max}>String length rangeMinLength<min> & MaxLength<max>
code: string & tags.Length<2>
score: number & tags.Range<{ min: 0, max: 100 }>
name: string & tags.Between<{ min: 1, max: 255 }>

// With custom error:
score: number & tags.Range<{ min: 0, max: 100, error: "Score out of range" }>

On this page