Skip to content

Refactoring ContextTypes to union type (instead of enum) #1138

@andreifloricel

Description

@andreifloricel

Currently the standard FDC3 Context types are defined as an enum:

export enum ContextTypes {
Chart = 'fdc3.chart',
ChatInitSettings = 'fdc3.chat.initSettings',
ChatRoom = 'fdc3.chat.room',
Contact = 'fdc3.contact',
ContactList = 'fdc3.contactList',
Country = 'fdc3.country',
Currency = 'fdc3.currency',
Email = 'fdc3.email',
Instrument = 'fdc3.instrument',
InstrumentList = 'fdc3.instrumentList',
Interaction = 'fdc3.interaction',
Nothing = 'fdc3.nothing',
Organization = 'fdc3.organization',
Portfolio = 'fdc3.portfolio',
Position = 'fdc3.position',
ChatSearchCriteria = 'fdc3.chat.searchCriteria',
TimeRange = 'fdc3.timerange',
TransactionResult = 'fdc3.transactionResult',
Valuation = 'fdc3.valuation',
}

This causes problems when constructing mapped and/or conditional types, e.g. generically typing a function so that for a given ContextType the corresponding Context structure is returned (fdc3.chart'` -> `Chart`, fdc3.action'->Action`, etc.).

The ContextType defined here is not helping, as it's NOT a valid union type:

export type ContextType = ContextTypes | string;

Also, in current versions of TypeScript everything enum types offer can be achieved with union types (and much more) without the inherent problems that TypeScript Enums have: https://www.typescriptlang.org/docs/handbook/enums.html#const-enum-pitfalls

My suggestion is to replace the current implementation with a simple union type.
Possibly also clearly differentiate between standard FDC3 context types and custom ones:

export type StandardContextTypes =
  | 'fdc3.action'
  | 'fdc3.chart'
  | 'fdc3.chat.initSettings'
  | 'fdc3.chat.message'
  | 'fdc3.chat.room'
  | 'fdc3.chat.searchCriteria'
  | 'fdc3.contact'
  | 'fdc3.contactList'
  | 'fdc3.country'
  | 'fdc3.currency'
  | 'fdc3.email'
  | 'fdc3.instrument'
  | 'fdc3.instrumentList'
  | 'fdc3.interaction'
  | 'fdc3.message'
  | 'fdc3.organization'
  | 'fdc3.portfolio'
  | 'fdc3.position'
  | 'fdc3.nothing'
  | 'fdc3.timerange'
  | 'fdc3.transactionResult'
  | 'fdc3.valuation';

export type ContextTypes = StandardContextTypes | string;

Currently, I need to define these types in all applications that interact with FDC3, but it would be nice to have them in the main library.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions