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";
 
const member: IMember = typia.random<IMember>();
const byte: Uint8Array = typia.protobuf.encode<IMember>(member);
console.log(byte);
 
interface IMember {
  id:
    | (string & tags.Sequence<11>)
    | (number & tags.Type<"uint64"> & tags.Sequence<12>)
    | (Uint8Array & tags.Sequence<13>);
  name: (string & tags.Sequence<20>) | null;
  children: Array<IMember> & tags.Sequence<30>;
  keywords: Map<string, string> & tags.Sequence<40>;
  thumbnail:
    | (string & tags.Format<"uri"> & tags.ContentMediaType<"image/*">)
    | Uint8Array;
  email: string & tags.Format<"email">;
  hobbies: Array<IHobby>;
}
interface IHobby {
  id: string & tags.Format<"uuid">;
  name: string;
  valid: boolean;
}

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<IMember>();
 
interface IMember {
  id:
    | (string & tags.Sequence<11>)
    | (number & tags.Type<"uint64"> & tags.Sequence<12>)
    | (Uint8Array & tags.Sequence<13>);
  name: (string & tags.Sequence<20>) | null;
  children: Array<IMember> & tags.Sequence<30>;
  keywords: Map<string, string> & tags.Sequence<40>;
  thumbnail:
    | (string & tags.Format<"uri"> & tags.ContentMediaType<"image/*">)
    | Uint8Array;
  email: string & tags.Format<"email">;
  hobbies: Array<IHobby>;
}
interface IHobby {
  id: string & tags.Format<"uuid">;
  name: string;
  valid: boolean;
}

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.