Rank-and-file government workers won big over union bosses Wednesday, when the US Supreme Court ruled 5-4 in favor of Mark Janus, an Illinois state worker who refused to join the American Federation of State, County and Municipal Employees. The court struck down an Illinois law that allowed the union to deduct fees from Janus’ paycheck despite his refusal to join.

The Janus ruling smashes laws in 22 states — including New York, Connecticut, New Jersey and California — that compel nonmembers to support unions. Until now, if you wanted a government job in these states, you had to pay up. But now firefighters, teachers and other public employees won’t have to fork over a penny to a union if they choose not to join. For the average worker who opts out, it’ll mean hundreds of dollars more in take-home pay a year.

More in workers’ pockets, less in union coffers. Nationwide, unions are expected to forfeit hundreds of millions of dollars when nonmembers stop paying and some members quit to save money, too.

When unions lose, so do politicians — primarily Democrats — who feed at their table. The Democratic Party counts on unions to tilt elections with hefty donations and get-out-the-vote manpower. Unions are the Democratic Party’s ATM. The Janus ruling, by reducing that money source, levels the political playing field.

One Republican is already a winner — President Trump — for delivering on his promise of a conservative court. In a 2016 Supreme Court case over union fees, the justices split 4-4, leaving mandatory fees in place. This time Trump’s Supreme Court pick, Neil Gorsuch, cast the tiebreaking vote.

Unions and their backers pointed out that, in theory, nonmembers could request a refund of any fees unions admitted spending on politics. But unions insisted little of it was spent that way, a deeply dishonest argument. Janus argued that everything a government union like AFSCME does is political.

Janus blamed AFSCME for the costly labor contracts and huge public budgets that Illinois taxpayers had to pay for. The justices decided that compelling Janus to support the union violates his constitutional rights under the First Amendment.

Justice Samuel Alito, writing for the majority, discarded a 1977 precedent and went straight to the Founders. Quoting Jefferson, Alito said, “To compel a man to furnish contributions of money for the propagation of opinions which he disbelieves and abhors is sinful and tyrannical.”

Unions are apoplectic. The National Education Association plans to slash its budget, fearing 300,000 members will quit.

Union bosses and their political allies aren’t taking the ruling lying down. Gov. Andrew Cuomo’s been touting his “anti-Janus” legislation, designed to make it nearly impossible for workers to opt out of unions.

New Jersey Gov. Phil Murphy also signed an end-run around Janus. It shrinks the opt-out window to a mere 10 days each year.

New Jersey’s anti-Janus law also requires new hires to sit through a mandatory 30-minute lecture on the benefits of union membership. Employers are barred from presenting the other side. In America? Is this crazy or what?

These laws, which show disdain for working people, are dead in the water. The justices anticipated such shenanigans. Their ruling specified that union fees cannot be extracted from a nonmember unless “the employee affirmatively consents to pay.”

Forget about trying to stop workers from opting out — unions will have to convince non-members to opt in.

Politicians who pushed anti-Janus provisions are the same ones who support lavish union contracts and work rules that explode the cost of public services.

In Connecticut, Democratic House Speaker Joe Aresimowicz sets the rules for union negotiations. Unbelievably, he’s a member of AFSCME himself. Thanks to this cronyism, Connecticut has the second-highest state- and local-tax burden in the nation after — you guessed it — New York.

The shameful politicians who plotted to weasel around the Janus ruling were blocked Wednesday. Janus will have impact. Workers can choose to support a union or spend their pay in other ways. With union political clout diminished, John Q. Taxpayer’s views will count for more. It’s a promising first step toward lowering the cost of government in tax-hell states like New York.

Betsy McCaughey is a senior fellow at the London Center for Policy Research.