This is the main driver for me in considering a switch to Apple. I almost did the last time I needed to upgrade, but the trade-in offers were good enough that I didn't. It doesn't feel like an upgrade though, I lost the back fingerprint scanner that worked great, but at least I get the security updates for a bit longer.
While I use an iPhone and would recommend it , you could easily get a phone like the pixel , one plus , etc that support lineageos or other 3rd party roms and that will keep support years after the vendor does. I will probably go that route next time because it’s not like the phone is unusable or slow after no more updates.
I have been on pixel my last few phones (currently on 6a). While I've done 3p roms on my phones before, I'm not as interested in tinkering with them anymore.