I'm currently exploring how to correctly define the return value when working with an object that includes optional formatter functions.
Everything is running smoothly for a function with a single value.
type Params = {
id?: number
created?: string
}
type FormatFn<TParam extends keyof Params, TValue> = (data: Params[TParam]) => TValue
type Result<TValue> = {
param: TValue
}
declare function query<TParam extends keyof Params, TValue = Params[TParam]>(
param: TParam,
formatter?: FormatFn<TParam, TValue>
): Result<TValue>;
// The functionality works as intended, where 'created' is typed as 'Date'
const { param: created } = query('created', (created) => new Date(created || ''))
I am interested in creating a version where you can input both an array of strings and an object containing optional formatter functions.
Here is a Playground showcasing my attempt.
This is what I expect as the output:
queries(['id', 'created'], {
created: (created) => new Date(created || '')
})
// Instead of the original return type
// {
// params: {
// id: number,
// created: string
// }
//
// I am aiming for this structure due to the formatter function
// {
// params: {
// id: number,
// created: Date
// }
// }