0.1.14 • Published 4 months ago

event-notifier v0.1.14

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

event-notifier

typed event emitter

to install the package

npm install event-notifier

after that you can use

type Data = {
  balance: { value: number };
  test: undefined;
};

const eventNotifier = new EventNotifier<Data>();

eventNotifier.on('balance', ({ value }) => {
  // your code
});

eventNotifier.notify({ type: 'test' });

eventNotifier.notify({ type: 'balance', value: 42 });

data object

type Data = {
  balance: { value: number };
  test: undefined;
};

const eventNotifier = new EventNotifier<Data>();

eventNotifier.on('test', (data) => {
  // Object.entries(data).length === 0;
});

eventNotifier.on('balance', (data) => {
  // Object.entries(data).length === 1;
  // 'value' in data === true;
  // typeof data.value === 'number'
});

erroneous statements

type Data = {
  balance: { value: number };
  test: undefined;
};

const eventNotifier = new EventNotifier<Data>();

// An argument of type ""asd" cannot be assigned to a parameter of type "keyof Data".ts(2345)
eventNotifier.on('asd', () => {});

// The "value" property is missing in the type "{ type: "balance"; }" and is required in the type "{ value: number; }".ts(2345)
eventNotifier.notify({ type: 'balance' });

// An object literal can only use unique properties. "data" does not exist in type "{ type: "test"; }.ts(2353)
eventNotifier.notify({ type: 'test', data: 42 });

// Type ""asd"" cannot be assigned to type "keyof Data".ts(2322)
eventNotifier.notify({ type: 'asd' });
0.1.14

4 months ago

0.1.13

4 months ago

0.1.12

4 months ago