#err instanceof Error ? 'shortMessage' in err ? err.shortMessage

14 messages · Page 1 of 1 (latest)

rancid boltBOT
#
adraffy#0

Preview:ts try { throw new Error() } catch (err) { const s: string = err instanceof Error ? "shortMessage" in err ? err.message : err.message : String(err) }

formal aurora
#

playground 5.8.2 shows no error

#

vscode 5.8.2 shows type error

#

seems like vscode is propagating the wrong type, shown in the tooltip

wintry nova
meager widget
#

Also are there any modifications to the Error type in your environment?

#

Oh, no, wait it fails in the playground too the code is just wrong - the playground had err.message on both branches

rancid boltBOT
#
retsam19#0

Preview:ts try { throw new Error() } catch (err) { const s: string = err instanceof Error ? "shortMessage" in err ? err.shortMessage : err.message : String(err) }

wintry nova
#

ah

meager widget
#

AFAICT shortMessage is not a property on Errors, you're checking that the property exists "shortMessage" in err, but you're not checking that it's a string.

wintry nova
#

you can add another check to solve that

rancid boltBOT
#
that_guy977#0

Preview:ts try { throw new Error() } catch (err) { const s: string = err instanceof Error ? "shortMessage" in err && typeof err.shortMessage === "string" ? err.shortMessage : err.message : String(err) }

formal aurora
#

ok yeah brainfart, the shortMessage type is unknown