This is based on an inaccurate analysis from a non-expert.
Switch game cards use Macronix XtraROM. These games are rated by the company for data retention shelf life of 20 years at 85°C / 185°F. But it depends on environmental conditions like humidity, and 20 years is an extremely conservative estimate. It's safe to say they will last much longer than that.
DS and 3DS games use an older version of Macronix technology, and critically also write to the cartridge itself. These will likely have issues long before Switch games do, although it should still be a long time.
Magnetic media was rated for a similar lifespan and in most cases still works fine 50 years later. The rare issues with CDs are due to manufacturing defects like glue separation, not the technology itself. These are, again, exacerbated by environmental factors. CDs are 30 years old at this point and the worst iteration of optical media, and the overwhelming majority work fine.
Different technologies, but just included for comparison.
If you're concerned about it, dump them to SSD RAID storage.