michaelbe812/angular-kit

Rx-stateful: stricter typing per context

Closed this issue · 0 comments

This will improve type narrowing, especially in templates.

RxStatefulWithValue/Error/Suspense

type Context = 'suspense' | 'error' | 'next';

interface Stateful<T, E> {
  value: T | null;
  hasValue: boolean;
  error: E | undefined;
  hasError: boolean;
  context: Context;
  isSuspense: boolean;
}

interface StatefulWithSuspense<T> {
  value: T | null;
  hasValue: boolean;
  hasError: false;
  error: undefined;
  // suspense
  context: 'suspense';
  // true
  isSuspense: true;
}

interface StatefulWithValue<T> {
  value: T;
  // true
  hasValue: true;
  //false
  hasError: false;
  error: undefined;
  // next
  context: 'next';
  // false
  isSuspense: false;
}
interface StatefulWithError<T, E> {
  value: null;
  // true
  hasValue: false;
  //false
  hasError: true;
  error: E;
  // next
  context: 'error';
  // false
  isSuspense: false;
}

type TODO<T, E> =
  | StatefulWithSuspense<T>
  | StatefulWithValue<T>
  | StatefulWithError<T, E>;

https://stackblitz.com/edit/typescript-mjrqck?file=index.ts