This question piqued my curiosity and prompted me to wonder about this issue: "Can True Resurrection be used to raise a creature whose body is still intact, but not within the range of touch?"
(Another older related question: "What happens when a caster targets an object that looks like a creature with a spell that targets only creatures?")
The issue of what happens when you cast a spell on an invalid target was initially unanswered by the PHB or DMG. Xanathar's Guide to Everything eventually provided an optional rule to address this question (p. 86, under "Invalid Spell Targets"):
If you cast a spell on someone or something that can’t be affected by the spell, nothing happens to that target, but if you used a spell slot to cast the spell, the slot is still expended. If the spell normally has no effect on a target that succeeds on a saving throw, the invalid target appears to have succeeded on its saving throw, even though it didn’t attempt one (giving no hint that the creature is in fact an invalid target). Otherwise, you perceive that the spell did nothing to the target.
(The answer to the question "Does a wand lose a charge if it attempts to target an invalid target?" suggests that just as a spell slot would be expended if cast using a spell slot, a charge would be expended if casting from a wand (or other magic item that used charges to cast spells).)
However, this still leaves something unclear:
What happens to material components that would normally be consumed, if the spell target is invalid? Are they still consumed?
For example, take a look at true resurrection, as in the case of the question that inspired this one:
You touch a creature that has been dead for no longer than 200 years and that died for any reason except old age. If the creature's soul is free and willing, the creature is restored to life with all its hit points.
The spell's material components are:
(a sprinkle of holy water and diamonds worth at least 25,000 gp, which the spell consumes)
What if the creature has been dead for over 200 years, or it died of old age? What if its soul is not free or willing? Would the 25,000 gp worth of diamonds still be consumed?
The question applies to any spell with consumed components, whether they are costly or not.