It will happen when you least expect it to, and when you least need it to. Such is the way of the Laws of Sod.
Both times a mech’s gone on me has been late on in a race: 4am at Sleepless and 85km into a 100km MTB-O event. Both times were pretty much user error combined with environmental factors (mud and bits of tree) as well, so not so much ‘component failure’ as ‘somewhat exceeded design spec limitations’. I’ve never had a shifter fail, but a freewheel went on the first climb of another MTB-O event. I don’t think there’s any way of second guessing it, unless you inspect vigorously for fatigue marks and check the internals of your shifters after every ride.
If you’ve only had the bike 18 months, unless you’re putting massive mileage on it, you shouldn’t be having bits ‘fail’ left right and centre.