Theo's Typesafe CultTTC
Theo's Typesafe Cult3y ago
7 replies
Rhys

Multiple Generics In Typescript Type

Building a library that lets you pull in all of your analytics events and make Typesafe queries of them, running into a problem with multiple generics


Events is a key, object pair which stores information about the events

type AnalyticsEvents = typeof events;

type AnalyticsEvent = AnalyticsEvents[keyof AnalyticsEvents];

type SamplingOptions = "TotalCount"

type Filter<T extends keyof AnalyticsEvents> = {
  name: AnalyticsEvents[T]["properties"][number]["name"];
  compare: "equals";
  value: string | number | boolean;
};

type SeriesEntry<T extends keyof AnalyticsEvents> = {
  name: T;
  sampling?: SamplingOptions;
  where?: Filter<T> | Filter<T>[];
};

type InsightOptions<T extends keyof AnalyticsEvents> = {
  events: SeriesEntry<T> | SeriesEntry<T>[];
  filters?: [];
  breakdownBy?: AnalyticsEvents[T]["properties"][number]["name"];
};

function fetchInsight<T extends keyof AnalyticsEvents>(
  options: InsightOptions<T>
) {}


Given above, the following should error in the second where since Answer Overflow Acount Id doesn't exist on autocapture, but since I can only do one generic it uses the combined type for both
fetchInsight({
  events: [
    {
      name: "User Grant Consent",
      sampling: "TotalCount",
      where: {
        name: "Answer Overflow Account Id",
        compare: "equals",
        value: 123,
      },
    },
    {
      name: "$autocapture",
      where: {
        name: "Answer Overflow Account Id",
        compare: "equals",
        value: 123,
      },
    },
  ],
});
Was this page helpful?