This may have been posted before since it's so easy to find out yourself.
When you use Gnomish Death Ray you will get a buff with the icon of a shadowbolt. Remove this buff and you wont take any dmg while the trinket is charging
edit: this macro will use the trinket and then remove the buff at the same time.
/use Gnomish Death Ray
/cancelaura Gnomish death ray