2

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))
1

1 Answer 1

2

Given a curried getActionType function declaration

declare const getActionType: <TPrefix, TActionTypeKeys extends string>(
  keys: TActionTypeKeys[]
) => (
    prefix: TPrefix
  ) => Record<TActionTypeKeys, [TPrefix, TActionTypeKeys]>

, the generic type parameter TPrefix is not preserved for the inner function, but instead instantiated with a default unknown type. TS looks for a code location to infer its type argument in the outer function signature of getActionType and falls back to unknown, because TPrefix is unused here. Example:

declare const apiActionTypeKeys: ApiActionTypeKeys[]

// (prefix: unknown) => Record<ApiActionTypeKeys, [unknown, ChangedActionTypeKeys]>
const createChangedctionType = getActionType(apiActionTypeKeys)

We can fix that by defining TPrefix on the inner function.

declare const getActionType: <TActionTypeKeys extends string>(
    keys: TActionTypeKeys[]
) => <TPrefix>(prefix: TPrefix) => Record<TActionTypeKeys, [TPrefix, TActionTypeKeys]>

And test it:

declare const apiActionTypeKeys: ApiActionTypeKeys[]
declare const changedActionTypeKeys: ChangedActionTypeKeys[]

// <TPrefix>(prefix: TPrefix) => Record<ApiActionTypeKeys, [TPrefix, ApiActionTypeKeys]>
const createApiActionType = getActionType(apiActionTypeKeys)

// "requested" | "completed" | "failed" | "cancelled"
const result = createApiActionType("fooPrefix").cancelled[1]

Full example

Hope, it helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.