Background: I've hired many many people on H1B and helped friends on visas start businesses.

TL;DR - In practice, this is very difficult to pull off. There are some chicken and egg issues that make it very difficult and the government is a pain in the ass about it too.

The rule of thumbs are that you cannot control the business that is giving you a visa (you can be fired from the job), the company has to demonstrate significant financial resources to employ you, and you have to do the transfer in a way that doesn't raise red flags with the government. Let's break these down.

1 - you cannot have a controlling interest in the business. This means you need a cofounder or cofounders so that your share of the business is less than 50%. Finding cofounders is tough. Especially cofounders who want to run the risk of you being deported during the next 4 years. Yes, there are ways to do it so that you own 49% and your cofounder owns 51%, but then you better be really sure that you want to give up board control and company control to this other person. This also means that your cofounder has to be a citizen or green card holder. Lots of constraints...

2 - financial resources either means you're putting up a million dollars of your own money or you've gotten an investor to give you $500k. But why is an investor going to give you $500k without a product or cofounder ready to go, and with a chance that you may run into visa issues and not be able to run the business? And therein is the chicken-and-egg situation. You can't stay without a financially sound company. But no one will give you money if you can't stay. So most people wait till they have a green card or build something on the side until they can get some traction or get some clients so they can show enough traction to raise funding.

3 - we almost got screwed with one of our employees at Spool because the government thought we didn't exist. Pre-launch we didn't have a website, our address was a UPS box, and though we were funded and clearly real, the USCIS basically rejected the visa transfer for one of our employees because they thought we were a shell company setup just for this guy to stay in the US without working.

Ultimately, there are people in an office at USCIS that make these decisions. These people will reject applications if something smells fishy, will reject it if they think this is a shell corporation just so you can stay in the US, or if they don't understand that startups get started and hire employees in month 1 of existence. Keep in mind that this transfer has to happen quickly. Your previous employer has something like 30 days to report that you don't work there any more. And your new employer gets to keep you in the country as long as the visa transfer is in process, but if you get rejected you're out of the country. So the transfer needs to happen quickly...and often your company just got started, has no other employees, has no website or product in the marketplace yet, and various other red flags as far as USCIS is concerned. The sequencing of all of these events in practice is difficult.

===== The best option =====
There are lots of "clever" options. I don't endorse any of them and you try them at your own risk. The safest option is to try and start something on the side, get enough traction to be able to pull together a seed round, and then have that new company sponsor your h1b. Lots of investors will give you money if you demonstrate the beginnings of a real business. This is extremely difficult though...you will basically need to work two full time jobs until you can raise a seed round.

I really wish it weren't this way but unfortunately the US government's rules were built in a manufacturing era and make no sense in our modern information age.

Disclaimer: This is not legal advice. Go talk to an immigration lawyer for real legal advice.