How can I retrieve the return type of map and only display S?
const initialState = {
x: 1,
y: 2
}
type InitialStateType = typeof initialState
type MapGetter<S, R, R1> = {
map: (state: S) => R
mapB?: (state: S) => R1
// more ...
}
function getMapResults<S extends MapGetter<any, any, any>>(
data: S
): ReturnType<S['map']> & ReturnType<S['mapB']> {
return {
...data.map(initialState),
...data.mapB?.(initialState)
// more ...
}
}
// Example usage with less explicit typing
const result = getMapResults({
map: (state: InitialStateType) => ({
x: state.x
}),
mapB: (state: InitialStateType) => ({
y: state.y
})
// more mappings...
})
// Desired output // state will be <{ x: 1, y: 2, z: 1 }>
const newResult = getMapResults<InitialStateType>({
map: (state) => ({
x: state.x,
z: state.x
}),
mapB: (state) => ({
y: state.y
})
})