MBP 2017 making high pitched noise when in Target Disk Mode
There are many reasons for high pitched noises (commonly referred to as electrical squeal).  However, without hearing it and being able to put hands on, it's impossible to diagnose.

Try a higher quality Thunderbolt 3 cable.  An active cable which will handle higher bandwidth and more power is a good bet.
Determine if it's coming from the speakers by manipulating the volume.  If it is, some part of the audio circuitry is picking up noise and amplifying it.  This is a logic board issue.
It could be a shielding issue - as silly as it sounds, you might be able to solve this by wrapping the cable in aluminum foil.

The OS and TDM mode aren't going have any effect on electrical noises; these are created when electrons move through a conductor and they can sometimes produce a squeal or a whine. However, the bottom line is that your Mac is not supposed to be doing that.  If it is, there's likely a failed component and it must be taken in for service.