XRPLF / xrpl.js

A JavaScript/TypeScript API for interacting with the XRP Ledger in Node.js and the browser
https://xrpl.org/
1.2k stars 510 forks source link

Stream event typings missing #1306

Closed varanauskas closed 2 years ago

varanauskas commented 4 years ago

As the project supports TypeScript, it would be nice to have typed stream events.

For example I could not find the type (interface) of LedgerEvent in the code (neither exported nor internal). However, there are JSON schemas for the events: https://github.com/ripple/ripple-lib/blob/d82703f41b41153f647084a50013bad35f301f9c/src/common/schemas/output/ledger-event.json

PR1: A good first step would be providing the types of such events exported standalone, for example

// definition
export interface LedgerEvent {
  ledgerVersion: number;
  // ...
}

// usage
import { RippleAPI, LedgerEvent } from "ripple-lib";
const api = new RippleAPI({ /* ... */ });
api.on('ledger', (ledger) => console.log((ledger as LedgerEvent).ledgerVersion));

PR2: After the types are defined TypeScript function overloads could be used to type the EventEmitter api in a way that would only allow specified event names and automatically get the type of the event callback similar to how the request method is typed already.

mvadari commented 2 years ago

Resolved by #1576