Then don't buy.
Or, better, work the numbers. Various expensive items (roofs, HVAC units, etc.) have fairly predictable lives. You might find that some of the expensive items have already been replaced.
I just rehabbed a 40-year-old townhouse. There really weren't any big surprises. We redid the kitchen, refinished the floors, converted a downstairs room from paneling and dropped ceiling to drywall, etc.
A 40-year-old townhouse should be structurally sound. You may find that you need to upgrade the electrical, simply because 40 years ago homeowners didn't have all the appliances they have today. We didn't have to do that in the townhouse, but I also rehabbed a single-family home, about 60 years old, that did need some electrical upgrades.
Get a home inspector in there to give you an idea of the likely remaining usable life of the systems. Then work the numbers.
Hope that helps.