📖 Guide Documents
Protocol Buffer
decode() functions

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";
 
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 data: ICustomer = typia.random<ICustomer>();
const encoded: Uint8Array = typia.protobuf.encode(data);
typia.protobuf.decode<ICustomer>(encoded);

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 encode = typia.protobuf.createDecode<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.decode<T>() related functions.