Skip to content

[RFC/Discussion] Use Typescript enums, objects and interfaces for storing data. #43

@jglover

Description

@jglover

When working with large data sets, I have found integrity and visibility to be a problem. It is very easy to make a mistake where an incorrect number is used. This is made more confusing by the fact there are numeric indexes as strings and it would be an easy mistake to zero-index the reference.

Since the project is already in Typescript, I'd like to propose that the data format switch from a JSON file to exported constants with interfaces and enums.
This has a few benefits:

  • Type safe (interfaces and enums)
  • Data integrity (data must be well defined, duplicates, typos etc are caught by linters and at compile time.
  • Higher visibility of data
  • Massively reduces refactoring workload and merge conflicts

Take for example:
clients/data/talks.json

{ 
  ...,
  "180": {
  "speakers": [187],
  "main_title": "Lightning launch - TakeShape",
  "alternative_titles": [],
  "categories": [15],
  "video_url": "https://youtu.be/ov9nZ2uBr2A",
  "video_upload_date": "2019-10-17",
  "conferences": [6]
 },
}

Now becomes
clients/data/talks.ts

export enum Speaker {
  'Example Person' = 'Example Person' , // string enum to make display easier.
  ...
}

export enum Category {
  React
}

export enum Conference {
  'Frontend Love Amsterdam 2018'
}

export interface IConferenceTalk {
  speakers: Speaker[],
  main_title: string
  alternative_titles: string[],
  categories: Category[],
  video_url: string,
  video_upload_date: Date,
  conferences: Conference[]
}

// Visibility of the relationships here is much improved
export const CONFERENCE_TALKS: {[number]: IConferenceTalk} = {
  "180": {
    "speakers": [Speaker.'Example Person'],
    "main_title": "Lightning launch - TakeShape",
    "alternative_titles": [],
    "categories": [Category.React],
    "video_url": "https://youtu.be/ov9nZ2uBr2A",
    "video_upload_date": new Date('2019-10-17'),
    "conferences": [Conference.'Frontend Love Amsterdam 2018']
   },
}

This would also make logic simpler in the React components that do the rendering, as they could just refer to myEnum[value] to get values.
Potentially JSON could still also be output at compile time or used as an input format to a parser than then validates the input, the downside to that option is it would still be easy to use an incorrect index.

If indexes were dropped from categories, speakers and conferences (possibly preserve them on conference talks), this would also massively reduce the number of merge conflicts and refactor work when there becomes a "race condition" on PRs using the same indexes.

I'd be interested in your thoughts

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions