gun icon indicating copy to clipboard operation
gun copied to clipboard

Typescript: Ack.err not typed correctly

Open code3z opened this issue 2 years ago • 1 comments

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?

code3z avatar Mar 12 '22 22:03 code3z

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 ;)

philippedasilva-orizone avatar Mar 23 '22 01:03 philippedasilva-orizone