callbag / callbag

👜 A standard for JS callbacks that enables lightweight observables and iterables
Other
1.56k stars 40 forks source link

Stricter types? #64

Open tim-smart opened 2 years ago

tim-smart commented 2 years ago

Would the Typescript community benefit from stricter types? It saves a few headaches when your compiler can hold your hand a bit more :)

A few things I would be looking for:

Here is something I have been using:

export enum Signal {
  START = 0,
  DATA = 1,
  END = 2,
}

export type TalkbackArgs = [signal: Signal.DATA] | [signal: Signal.END]
export type Talkback = (...op: TalkbackArgs) => void

export type SinkArgs<E, A> =
  | [signal: Signal.START, talkback: Talkback]
  | [signal: Signal.DATA, data: A]
  | [signal: Signal.END, error?: E]
export type Sink<E, A> = (...op: SinkArgs<E, A>) => void

export type SourceArgs<E, A> = [signal: Signal.START, sink: Sink<E, A>]
export type Source<E, A> = (...op: SourceArgs<E, A>) => void

export type Callbag<E, A> = Source<E, A> | Sink<E, A>
tim-smart commented 2 years ago

Quick update, created a small package for this: https://github.com/tim-smart/strict-callbag

I also added an error type to the talkback arguments, as often a sink can fail with an error (writing a file to disk can fail etc)