Howdju / howdju

Monorepo for the Howdju crowdsourced fact checking and summarization platform
https://www.howdju.com
GNU Affero General Public License v3.0
5 stars 2 forks source link

Solve timestamp contradiction between types and Redux #484

Open carlgieringer opened 1 year ago

carlgieringer commented 1 year ago

Redux doesn't like non-serializable values in the store (or in actions.) But our entities use Moment for dates. The entities come from the API as timestamp strings, but this is problematic because 1) strings are less convenient for date comparison/formatting, and 2) this type is incompatible with the server-side entity, making cross-platform helper methods more complicated (would have to type them to handle objects having either strings or Moments at each time field.)

For now, we are starting to convert our entity timestamps to Moment objects in entities.js. This provides convenience and type compatibility. But it violates Redux's rule, so I'm worried that it's not a viable long-term solution.

This is an approach of typing entities in the client as using strings for time. But it is problematic because now methods accepting UrlLocator don't accept UrlLocatorView.

export type ReplaceDeep<T, From, To> = T extends From
  ? To
  : T extends object
  ? { [K in keyof T]: ReplaceDeep<T[K], From, To> }
  : T;

export type UrlLocatorView = ReplaceDeep<UrlLocatorOut, Moment, string> & {
  /** A key uniquely identifying a url locator relative to others. */
  key: string;
};

Possible solutions:

Other notes:

carlgieringer commented 1 year ago

Related: #154