Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeScript ability to use a type's value and derive other value from keying off of type/interface map

type ListThemes =
  | "projects"
  | "data"
  | "alerts"
  | "jobs";

type ListDataType = {
  projects: ProjectListFieldsTypeMap;
  data: DataListFieldsTypeMap;
  alerts: AlertsListFieldsTypeMap;
  jobs: JobListFieldsTypeMap;
};

type ListProps = {
  theme: ListThemes;
  data: DERIVED_TYPE_HERE;
};

DERIVED_TYPE_HERE would be the value of ListDataType[theme]

Have made attempts to more directly tie ListThemes to the keys in ListDataType, but was not able to do so and still directly create a 1:1 relationship.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

something like this with generics might achieve your goal as I’m reading it:

type ListProps<T extends ListThemes> = {
  theme: T;
  data: ListDataType[T];
};

but this won’t work with the code you’ve given as GraftListThemes isn’t defined, so I’m not sure what it’s referring to, and entities doesn’t map to anything in ListDataType.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading