samchon / typia

Super-fast/easy runtime validators and serializers via transformation
https://typia.io/
MIT License
4.55k stars 157 forks source link

Feature request: typia.application<T>() to return IJsonApplication<T> #760

Closed roman-shpp closed 1 month ago

roman-shpp commented 1 year ago

Feature Request

I'm using typia not only to validate data, but also to extract type fields (or union values) to runtime array. E.g. typia makes it possible to do:

 console.log(unionValues(typia.application<[2 | 1 | 3]>()));
 --> [2, 1, 3] // unknown[]

however, i cannot properly type result of my "unionValues" function because it accepts IJsonApplication as parameter, so type cannot be extracted from there. Or, I'll have to specify the type multiple times to get something like that:

 console.log(unionValues<[2,1,3]>(typia.application<[2 | 1 | 3]>()));
 --> [2, 1, 3]   // number[]

My request: let's extend IJsonApplication like this:

export interface IJsonApplication<T> {
    schemas: IJsonSchema[];
    components: IJsonComponents;
    purpose: "swagger" | "ajv";
    type?: T | undefined; 
}

this "ephemeral" field will be always undefined during the runtime, but will provide a way to extract the original type from IJsonApplication object at compile-time.

What do you think?

BTW, typia.application can be used as base to create some other useful functions, like objectKeys, etc.

samchon commented 1 year ago

This suggestion is too private.

Just make your custom function and interface type like below.

Then you can accomplish what you want.

interface MyJsonApplication<Types extends unknown[]> extends typia.IJsonApplication {
    types?: Types[] | undefined; 
}

function something<Types extends unknown[]>(closure: () => IJsonApplication): MyJsonApplication<Types> {
    return closure();
}
roman-shpp commented 1 year ago

Your solution doesn't work, i still have to specify type 2 times instead of 1 time:

const a = something<[1, 2, 3]>(() => typia.application<[1, 2, 3]>());
//        ^? function something<[1, 2, 3]>(closure: () => IJsonApplication): MyJsonApplication<[1, 2, 3]>
const b = something(() => typia.application<[1, 2, 3]>());
//        ^? function something<unknown[]>(closure: () => IJsonApplication): MyJsonApplication<unknown[]>

so if I specify type only 1 time, then result has type unknown[]

samchon commented 9 months ago

Will suport this feature at v6 major update with breaking changes.