I think it's in your best interests to avoid disclosing it if you can do so without losing the opportunity. However once you've been selected and the company is negotiating an offer, disclosing your compensation to the employer (and not a recruiter) isn't so bad.
The two worst cases are that you lose the offer (because you're too expensive or too cheap) or get a low offer (because they are willing to pay more but decide to base their offer on your previous compensation). If you go your separate ways, you are free to start the negotiation process all over again with another employer.
Disclosing your compensation to a recruiter is an absolute no. You are instantly and immediately disclosing your compensation to every employer, for all time.