简体   繁体   中英

Create a Generic Type from enum

I'm trying to create a generic Type using enums.

Enum

export enum OverviewSections {
  ALL = 'all',
  SCORE = 'score_breakdown',
  PERFORMANCE = 'performance_over_time',
  ENGAGEMENT = 'engagement',
  COMPANY = 'company_views_graph',
  PEOPLE = 'people_views_graph',
  ARTICLES = 'articles_read_graph',
  PLATFORM = 'platform_sessions_graph',
  EMAILS = 'emails_opened_graph',
}

Now I'd like to create a generic type which would help me achieve something like this:

Overview: {
    [OverviewSections.ALL]: {
      data: IOverview | null,
      loading: boolean,
      error: boolean
    },
    [OverviewSections.SCORE]: {
      data: IScore | null,
      loading: boolean,
      error: boolean
    },
    [OverviewSections.PERFORMANCE]: {
      data: IPerformace | null,
      loading: boolean,
      error: boolean
    },
    ......
  },

How can I achieve this? Thanks

There is one thing You can do:


// I have replaced ENUM with IMMUTABLE object. It is much safer

const OverviewSections = {
  ALL: 'all',
  SCORE: 'score_breakdown',
  PERFORMANCE: 'performance_over_time',
} as const

// I created type for boilerplate code
type DataStatus<T> = {
  data: T | null
  loading: boolean,
  error: boolean
}

// Mocks for your interfaces
interface IOverview {
  tag: 'IOverview'
}
interface IScore {
  tag: ' IScore'
}
interface IPerformace {
  tag: 'IPerformace'
}

// You still need to map your types
type Mapped = {
  [OverviewSections.ALL]: IOverview;
  [OverviewSections.PERFORMANCE]: IPerformace;
  [OverviewSections.SCORE]: IScore
}

// This type will take all VALUES of OverviewSections,
// because we use them as a keys for our map
type Values<T> = {
  [P in keyof T]: T[P]
}[keyof T]

/**
 * This is the main type
 * 1) It maps through all OverviewSections values
 * 2) Checks if value is equal to Mapped keyof
 * 3) if Yes - create s DataStatus with appropriate generic
 * 4) if No - returns NEVER
 */
type MakeType<E extends Record<string, string>, M> = {
  [P in Values<E>]: P extends keyof M ? DataStatus<M[P]> : never
}

type Result =  MakeType<typeof OverviewSections, Mapped>

Don't worry, You can still use an enum instead of immutable object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM