1

I defined two types from Enum picking up values

goodValue?: `${ValueOf<typeof SampleEnum>}`;
goodValueWithExclude?: `${ExcludeValue<typeof SampleEnum, 'ONE'>}`;

export enum SampleEnum {
  ONE = 'One_Value',
  TWO = 'Two_Value',
}

Type defintions:

export type ValueOf<T> = T[keyof T];
export type ExcludeValue<T, K> = ValueOf<Pick<T, Exclude<keyof T, K>>>;

But I dont want to invoke like this - ${ValueOf<typeof SampleEnum>}. I want to invoke like ${ValueOf<SampleEnum>}. Looks Simpler. I could not make it work. tried a lot of ways.

dummy1?: `${ValueOf2<SampleEnum>}`;
dummy2?: `${ValueOf2Exclude<SampleEnum>}`;

export type ValueOf2<T, K> = ValueOf<Pick<typeof T, Exclude<keyof typeof T, K>>>;
export type ValueOf2Exclude<T> = (T) => ValueOf<typeof T>;

None of them work - expecting like the below:

AccountLessRequest.goodValue?: "Two_Value" | "One_Value" | undefined AccountLessRequest.goodValueWithExclude?: "Two_Value" | undefined

Mprem
  • 11
  • 3
  • Sorry, but that won't work for reasons I lay out in the answer to the linked question. You'll need to keep writing something like `ExcludeValue` and not something like `ExcludeValue`. – jcalz May 08 '22 at 01:42
  • can I use a function to get a type? and pass the enum ? – Mprem May 08 '22 at 13:49

0 Answers0