Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
roman-shpp opened this issue Aug 14, 2023 · 3 comments
Open
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed invalid This doesn't seem right question Further information is requested
Projects

Comments

@roman-shpp
Copy link

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 samchon self-assigned this Aug 16, 2023
@samchon samchon added invalid This doesn't seem right question Further information is requested wontfix This will not be worked on labels Aug 16, 2023
@samchon
Copy link
Owner

samchon commented Aug 16, 2023

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
Copy link
Author

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 samchon added the help wanted Extra attention is needed label Oct 27, 2023
@samchon samchon added this to To do in v6 Update via automation Jan 19, 2024
@samchon samchon added enhancement New feature or request and removed wontfix This will not be worked on labels Jan 19, 2024
@samchon
Copy link
Owner

samchon commented Jan 19, 2024

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

@samchon samchon removed this from To do in v6 Update May 9, 2024
@samchon samchon added this to To do in v8 Update via automation May 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed invalid This doesn't seem right question Further information is requested
Projects
Development

No branches or pull requests

2 participants