typia
typia copied to clipboard
Feature Request: typia.transform - delayed type specification
One of the limitations of typia
is the inability to set types as generics. This creates a number of constraints, which are unfortunately difficult to resolve. To mitigate some of these constraints, I propose the introduction of a typia.transform
function.
typia.transform
takes a single function f: (x: any) => any
as its argument. The returned value is promisified as the base typia
module, and data is processed through the argument function and awaited before being passed on. In other words, await typia.transform(f).is<Data>(x)
is equivalent to typia.is<Data>(await f(x))
. This function will allow for wrapping typia
without using generics, which is currently impossible due to the aforementioned limitations, forcing us to return the functions as they are.
Consider the following scenario: We wish to create a wrapper function for typia.assert
that takes a numeric ID, calls an API, and asserts the type of the response. Using the proposed method, we could create a wrapper function as follows:
const api = (x: number) => fetch('/api/account/' + x).then(x => x.json());
const wrapped = typia.transform(api).assert;
const data = await wrapped<IAccount>(3);
Currently, to achieve similar functionality, we must pass a checker function to each function as mentioned in issue #125, which is less convenient than the example provided.
If there are existing solutions that I have overlooked, I would greatly appreciate your input.
I think typia.prepare<T>()
function name would be better, then it needs to support every typia
features.
By the way, I'm sorry but cannot sure when to develop it.
export function prepare<T>(): Feature<T>;
interface Feature<T> {
is: (input: unknown): input is T;
assert: (input: unknown) => T;
protobuf: {
assertEncode: (input: T): Uint8Array;
};
}
can I work on this issue?
Of course why not?