Closed roman-shpp closed 1 month 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();
}
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[]
Will suport this feature at v6 major update with breaking changes.
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:
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:
My request: let's extend IJsonApplication like this:
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.