0.3.0 • Published 10 days ago

@ksv90/event-notifier v0.3.0

Weekly downloads
-
License
ISC
Repository
github
Last release
10 days ago

event-notifier

typed event emitter

to install the package

npm install @ksv90/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.emit({ type: 'test' });

eventNotifier.emit({ 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.emit({ type: 'balance' });

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

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

10 days ago

0.2.1

2 months ago

0.2.0

2 months ago

0.1.15

2 months ago