decode() functions

export namespace protobuf {
  export function decode<T>(buffer: Uint8Array): Resolved<T>;
  export function isDecode<T>(buffer: Uint8Array): Resolved<T> | null;
  export function assertDecode<T>(buffer: Uint8Array): Resolved<T>;
  export function validateDecode<T>(
    buffer: Uint8Array,
  ): IValidation<Resolved<T>>;
}

Protocol Buffer Decoder.

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

By the way, as Protocol Buffer handles binary data directly, there’s no way when input binary data was not encoded from the T typed value. In that case, unexpected behavior or internal error would be occured. Therefore, I recommend you to encode binary data of Protocol Buffer from type safe encode functions like below, Use typia.protobuf.encode<T>() function only when you can trust it.

For reference, typia provides type safe decorators like below, but they are just for additional type validation like number & Minimum<7> or string & Format<"uuid"> cases, that are represented by Special Tags. Thus, I repeat that, you’ve to ensure type safety when using decoder function.


protobuf.decode.ts
import typia, { tags } from "typia";
 
const member: IMember = typia.random<IMember>();
const encoded: Uint8Array = typia.protobuf.encode<IMember>(member);
const decoded: IMember = typia.protobuf.decode<IMember>(encoded);
console.log(member, decoded);
 
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 createDecode<T>(): (buffer: Uint8Array) => Resolved<T>;
  export function createIsDecode<T>: (buffer: Uint8Array) => Resolved<T> | null;
  export function createAssertDecode<T>(): (buffer: Uint8Array) => Resolved<T>;
  export function createValidateDecode<T>(): (
      buffer: Uint8Array
  ) => IValidation<Resolved<T>>;
}

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

If you repeat to call typia.protobuf.decode<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.createDecode<T>() function.

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

protobuf.createDecode.ts
import typia, { tags } from "typia";
 
export const decode = typia.protobuf.createDecode<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.decode<T>() related functions.