t4t5 / nostr-react

React Hooks for Nostr 🦤
MIT License
85 stars 14 forks source link

Potential type improvement #7

Open chmac opened 1 year ago

chmac commented 1 year ago

I think that there's a type mismatch. The type Event in nostr-tools has id and sig as optional. But when receiving an event from a relay, those two parameters must exist. So the type of events from a relay is, I believe, actually a Required<Event>.

I suspect that the type should be Required<NostrEvent> here:

https://github.com/t4t5/nostr-react/blob/55d9f5f4496b2898e6178c7d02f4ee56864a5f84/src/core.tsx#L136

It looks to me as though the type of what a subscription.on('event', cb) receives (so the type of the argument passed to cb) is not specified in nostr-tools. I think that means that by changing line 136 above to Required<NostrEvent> the type would be correct.

I noticed this because when I'm trying to read the id of events in React, TypeScript complains the id could be undfined. I don't think it can be when it comes from a relay.

Apologies if this is not very clear! I'll try to explain it more clearly if this doesn't make sense.