I want to access types dynamically for networking but there are other use cases.
My request is a typia.nameOf<T> function, that simply returns the name of the type T. E.g. typia.nameOf<number>() evaluates to "number".
I could achieve something similar using typia.is but it would be O(n). My proposal would allow an O(1) implementation.
For example,
type HelloPacket = { hello: string };
type HeyPacket = { hey: string };
type Wrapper = {
type: string;
data: Uint8Array;
};
function sendPacket<T>(data: T) {
typia.protobuf.encode<Wrapper>({
type: typia.nameOf<T>(),
data: typia.protobuf.encode<T>(data),
});
}
function onPacket<T>(callback: (data: T) => void) {
// packetHandlers comes from elsewhere
packetHandlers.set(
typia.nameOf<T>(),
callback
)
}
// Now a packet comes,
// I open the Wrapper where Wrapper["type"] is the name
// and call the relevant decoder made with typia.protobuf.createDecode
Feature Request
I want to access types dynamically for networking but there are other use cases.
My request is a
typia.nameOf<T>
function, that simply returns the name of the type T. E.g.typia.nameOf<number>()
evaluates to"number"
.I could achieve something similar using
typia.is
but it would beO(n)
. My proposal would allow anO(1)
implementation.For example,