ts-transformer-enumerate icon indicating copy to clipboard operation
ts-transformer-enumerate copied to clipboard

Feature request? Enumerating keys from a Pick of another type

Open dmb-agoradcm opened this issue 5 years ago • 2 comments

I know very little about the inner gubbins of TS, so my apologies if this is a stupid suggestion.

I have a type SubsetOfOtherType:

export interface OtherType {
  prop1: Currency;
  prop2: number;
  prop3?: Date;
  prop4: IndicativeNum;
}
export type SubsetOfOtherType = Pick<
  OtherType,
  "prop1" | "prop2" | "prop4"
>;

I want to check whether a key from the OtherType matches the subset, and I'd hope to be able to do enumerate<keyof SubsetOfOtherType>() to compare against but this fails.

As I understand it keyof provides a union (array?) of the keys as strings, which I perhaps naïvely assumed would work.

Thanks for your time!

dmb-agoradcm avatar Apr 17 '20 16:04 dmb-agoradcm

You are correct. The transformer seems not working as expected when keyof some type passed as a type parameter even though keyof provides a union of the prop names of the type. Thanks for reporting!

However, actually I am thinking of deprecating this transformer as I think const assertion can cover all the use cases of this transformer. Please refer to my answer on SO and check if the solution I provided there works for you. If it does not work or you are not sure how that can be used in place of this transformer in your use case, please describe your use case more specifically and I think I can help you.

kimamula avatar Apr 18 '20 03:04 kimamula

I'm not the OP, but I just came across this project while trying to iterate over a union of strings and then found this issue. In my case, I'm using Relay to generate TS definitions corresponding to GraphQL types and operations. For GraphQL enums, the compiler generates code like:

export type AwsTypeEnum = "S3" | "SSM" | "%future added value";

Since I don't control the generator and don't have much desire to maintain a fork of it, I was looking at this transformer as a means of iterating over the union's candidate values. It doesn't look like this plays well with your suggested replacement on SO, but please let me know if I have that wrong.

nirvdrum avatar Aug 12 '20 23:08 nirvdrum