đź“– Guide Documents
Protocol Buffer
encode() functions

encode() functions

export namespace protobuf {
  export function encode<T>(input: T): Uint8Array;
  export function isEncode<T>(input: T): Uint8Array | null;
  export function assertEncode<T>(input: T): Uint8Array;
  export function validateEncode<T>(input: T): IValidation<Uint8Array>;
}

Protocol Buffer Encoder.

You can easily convert a JavaScript object to a binary data of Protocol Buffer, without any extra Protocol Buffer Message Schema definition. typia.protobuf.encode<T>() function analyzes your type T, and generates a Protocol Buffer Message Schema internally. And then, it converts the input instance to the binary data of Protocol Buffer format.

By the way, typia.protobuf.encode<T>() function does not validate the input value. It just believes user and input value, and converts to the Protocol Buffer binary data directly without any validation. By the way, if the input value was not validate, the encoded binary data never can be decoded. So, if you can't sure the input value type, you should use below functions instead.


protobuf.encode.ts
import typia, { tags } from "typia";
 
interface ICustomer {
  id: number & tags.Type<"int32">;
  email: string & tags.Format<"email">;
  name: string;
  pet: null | ICat | IDog;
  memo: null | Map<string, string>;
  logins: Array<ICustomerLogin>;
}
interface ICat {
  type: "cat";
  name: string;
  ribbon: boolean;
}
interface IDog {
  type: "dog";
  name: string;
  hunt: boolean;
}
interface ICustomerLogin {
  success: boolean;
  href: string & tags.Format<"url">;
  referrer: string & tags.Format<"url">;
  ip: string & (tags.Format<"ipv4"> | tags.Format<"ipv6">);
  time: string & tags.Format<"date-time">;
}
 
const customer: ICustomer = typia.random<ICustomer>();
typia.protobuf.encode<ICustomer>(customer);

Reusable Functions

export namespace protobuf {
  export function encode<T>(): (input: T) => Uint8Array;
  export function isEncode<T>(): (input: T) => Uint8Array | null;
  export function assertEncode<T>(): (input: T) => Uint8Array;
  export function validateEncode<T>(): (input: T) => IValidation<Uint8Array>;
}

Reusable typia.protobuf.encode<T>() function generators.

If you repeat to call typia.protobuf.encode<T>() function on the same type, size of JavaScript files would be larger because of duplicated AOT compilation. To prevent it, you can generate reusable function through typia.protobuf.createEncode<T>() function.

Just look at the code below, then you may understand how to use it.

protobuf.createEncode.ts
import typia, { tags } from "typia";
 
export const encode = typia.protobuf.createEncode<ICustomer>();
 
interface ICustomer {
  id: number & tags.Type<"int32">;
  email: string & tags.Format<"email">;
  name: string;
  pet: null | ICat | IDog;
  memo: null | Map<string, string>;
  logins: Array<ICustomerLogin>;
}
interface ICat {
  type: "cat";
  name: string;
  ribbon: boolean;
}
interface IDog {
  type: "dog";
  name: string;
  hunt: boolean;
}
interface ICustomerLogin {
  success: boolean;
  href: string & tags.Format<"url">;
  referrer: string & tags.Format<"url">;
  ip: string & (tags.Format<"ipv4"> | tags.Format<"ipv6">);
  time: string & tags.Format<"date-time">;
}

References

Protocol Buffer supports special numeric types like int32 or uint64 that are not supported in TypeScript. Also, types of Protocol Buffer cannot fully meet TypeScript type specs either, as expression power of TypeScript types are much stronger than Protocol Buffer.

To know how to define special numeric types like uint64, and to understand which TypeScript types are not supported in Protocol Buffer specs, it would better to read below documents. I recommend you to read them before using typia.protobuf.encode<T>() related functions.