Is it possible to have a const value that takes generic argument?
For this code
import * as R from 'ramda';
enum ApiActionType {
requested,
completed,
failed,
cancelled,
}
type ApiActionTypeKeys = keyof typeof ApiActionType;
enum ChangedActionType {
changed,
}
type ChangedActionTypeKeys = keyof typeof ChangedActionType;
const getActionType = <TPrefix, TActionTypeKeys extends string>(
keys: TActionTypeKeys[]
) => (
prefix: TPrefix
): Record<TActionTypeKeys, [TPrefix, TActionTypeKeys]> => {
return R.pipe(
R.map(k => [k, [prefix, k]] as [TActionTypeKeys, [TPrefix, TActionTypeKeys]]),
R.fromPairs as () => Record<TActionTypeKeys, [TPrefix, TActionTypeKeys]>
)(keys);
}
// const createApiActionType: <TPrefix>(prefix: TPrefix) => Record<"requested" | "completed" | "failed" | "cancelled", [TPrefix, "requested" | "completed" | "failed" | "cancelled"]>
const createApiActionType = <TPrefix>(prefix: TPrefix) => getActionType<TPrefix, ApiActionTypeKeys>(R.keys(ApiActionType))(prefix)
// const createChangedctionType: <TPrefix>(prefix: TPrefix) => Record<"changed", [TPrefix, "changed"]>
const createChangedctionType = <TPrefix>(prefix: TPrefix) => getActionType<TPrefix, ChangedActionTypeKeys>(R.keys(ChangedActionType))(prefix)
Is it possible to simplify the last two lines to below without lost generic argument to the resulting function? i.e. preserve the TPrefix generic argument instead of become a non-generic function with unknown prefix type
// const createApiActionType: (prefix: unknown) => Record<"requested" | "completed" | "failed" | "cancelled", [unknown, "requested" | "completed" | "failed" | "cancelled"]>
const createApiActionType = getActionType(R.keys(ApiActionType))
// const createChangedctionType: (prefix: unknown) => Record<"changed", [unknown, "changed"]>
const createChangedctionType = getActionType(R.keys(ChangedActionType))