You can define a "collection" of TypeScript types using enums and interfaces, like this:

enum MyList {
  A,
  B,
  C
}

interface MyData {
  [MyList.A]: {color: string, enabled: boolean},
  [MyList.B]: string,
  [MyList.C]: any
}

Then, you can reference each data type like this:

function process(value: MyList, data: any) {
  switch (value) {
    case MyList.A:
      console.log((data as MyData[MyList.A]).color);
      break;
    case MyList.B:
      console.log((data as MyData[MyList.B]).toLowerCase());
      break;
    case MyList.A:
      console.log((data as MyData[MyList.C]));
      break;
  }
}
Final thoughts

This could definitely be cleaner and it is most likely not what interfaces were designed for but, for my purposes today, it'll do the job!

Latest on TypeScript
Mastodon Mastodon