Type of Stream - TypeScript pro needed!

Here is something I have been racking my head with the last few hours (with no success).

As mentioned in Using @daml/ledger with React Class Components I am trying to migrate to class components and hence cannot use hooks. I need to keep a Stream member in my class.

The actual type of Stream is pretty nasty like:
Stream<TransportationRecord, undefined, "827c178267266bb80711c560783b5391b5a3a45035176e251689366b6ce2ee85:Records:TransportationRecord", readonly CreateEvent<TransportationRecord, undefined, "827c178267266bb80711c560783b5391b5a3a45035176e251689366b6ce2ee85:Records:TransportationRecord">[]>

So far I have used this to get the type correct:

const getTransportationRecordStream = (ledger: Ledger) => ledger.streamQuery(TransportationRecord);
export type TransportationRecordStream = ReturnType<typeof getTransportationRecordStream>;

I don’t want to have to do this for every new type of Stream. So I started working on my own Stream class:

class Stream<Templ extends Template<any,any,any>, 
              T extends object=(Templ extends Template<infer A,any,any> ? A : never), 
              K extends  any  =(Templ extends Template<any,infer A,any> ? A : never),
              I extends string=(Templ extends Template<any,any,infer A> ? A : never)
            > {
  constructor(ledger: Ledger, template: Template<T, K, I>) {
    this.ledger = ledger;
    this.stream = ledger.streamQuery(template);

  private ledger: Ledger;
  private stream: DamlStream<T, K, I, readonly CreateEvent<T, K, I>[]>;

  onLive(func: () => void) {
    this.stream.on('live', func);

  onChange(func: (results: ReadonlyArray<CreateEvent<T, K, I>>) => void) {
    this.stream.on('change', func);

  onClose(func: (closeEvent: StreamCloseEvent) => void) {
    this.stream.on('close', func);

  close() {

This was in the hope that I could simply define my stream as stream: Stream<TransportationRecord>. Unfortunately I cannot get it to work; I think because TypeScript thinks I am using the export declare type TransportationRecord as opposed to the export declare const TransportationRecord from my DAML-generated javascript code.

Any help would be great! (TypeScript can be a real pain sometimes :’))

1 Like

Nevermind, think I solved it:

I can just create with stream: Stream<typeof TransportationRecord>

1 Like