How broadly they can claim your work as their IP may depend not only on your country, but also on finer grained jurisdictions within your country. Within the United States, this can vary from state to state.
California generally says that if you develop the work entirely on your own time with your own resources, then it's yours. There's a big caveat, however: If the invention relates to an existing business or area of research of the employer, then the employer can still claim it. Even if you weren't aware of the fact that your employer pursing that line of business.
If you work for a massive tech company with their fingers in everything and with all sorts of (sometimes secret) projects in development, then it's very hard to create anything that clearly escapes the caveat. The company will probably just assume they own everything.[*]
These big companies are more likely than smaller ones to have an internal review process, where you disclose your work to them and ask them to waive their copyrights. (I don't know of any company that will let you keep a patent, regardless of the field.) Most big tech companies also support open source work, so if you want permission to contribute to a couple favorite projects, ask.
If you join a small company with one or two well-known lines of business, even the broad claims are easier to navigate, and you can be reasonably certain your personal project doesn't overlap with the interests of your employer.
The contracts I've seen with broad claims also have a form you can fill out to list your prior works which are then explicitly excluded and the form is made part of the contract as an addendum.
Finally, if you are an independent contractor (as opposed to an employee), make very sure your client claims only the work they are paying you to create. If they try to claim more than that, point out that you might have multiple clients at any given time and that broad claims would be incompatible.
[*]: This leads to all sorts of absurd contradictions. Here's one example: You're a software engineer for Foo Tech, which assumes everything you create belongs to them. You use a vacation day to interview with Barsoft. Barsoft asks you to write a code sample as part of the interview. Who holds the rights to that code sample? Have you violated your current employment contract? Does the contract infringe upon your right to free expression? Does it interfere with your right to seek employment in your field?