amark / gun

An open source cybersecurity protocol for syncing decentralized graph data.
https://gun.eco/docs
Other
18.05k stars 1.16k forks source link

Typescript: Ack.err not typed correctly #1221

Open code3z opened 2 years ago

code3z commented 2 years ago

if (ack.err) throw new Error(ack.err) throws Property 'err' does not exist on type 'GunMessagePut'. Property 'err' does not exist on type '{ ok: { '': 1; }; }'.ts(2339)

The callback would probably be better typed as

export type GunMessagePut =
  | {
    /** if there was an error during save */
    err: string
  }
  & {
    /** if there was a success message (none is required though) */
    ok: { '': 1 }
  }

export type GunCallbackPut = (ack: GunMessagePut) => void

unless it's an error on my end?

philippedasilva-orizone commented 2 years ago

Have you tried this:

if('err' in ack) console.log(err)
else console.log(ack.ok)

I'm just asking because I didn't know about that trick on typescript Unions ;)