In 1940 when he wrote this Heinlein was a New Deal Democrat supporter of Franklin Roosevelt. He was an active progressive who had recently worked on Upton Sinclair’s socialist “End Poverty in California” campaign. His libertarian shift was twenty years in the future, in his fifties.
Heinlein's alleged libertarian shift. In the 1940s Heinlein was mentioned as a Social Credit supporter which implied he was always on the libertarian side of Socialism. Social Credit was the American party killed/disrupted by the red scare that believed in a UBI and universal health care. His posthumous published but first written novel "For Us, The Living" is even a wonderfully naive paean to UBI and credits a Social Credit party for its enactment and ensuing utopia.
Heinlein formally disowned some of those ideas, but did so under the duress of the McCarthy era and the Red Scare. Yet he also kept writing about them, just somewhat cloaked. Both "Stranger in a Strange Land" and "The Moon is a Harsh Mistress" are some of the most Socialist books I've ever read, if you assume the first person narrators are for the most part bloviating Vonnegut-esque patsies (rather than the author stand-ins they are often read to be; Heinlein seemed to clever for that). "Stranger in a Strange Land" is entirely about community effort and Socialism. It contrasts interestingly with "For Us, The Living" in part because cynicism seems to have been the big shift and Heinlein can no longer imagine America leading the charge towards Socialism and invents a dead Martian race to do it. "The Moon is a Harsh Mistress" is often credited as a deeply Libertarian book, but I think a lot of that is misreading the narrative and not paying attention to especially the second half of the book, which is entirely about AI-lead worker's strikes towards the goals of unionization. The first half sounds like a Libertarian dream and the narrator character describes it in lush terms that make it sound so, but plot is about overthrowing that and building a much more Socialist Moon together. Heinlein even comments about that misreading in "The Cat Who Walks Through Walls" which intentionally begins on a Moon like the one people reading the first half of "The Moon is a Harsh Mistress" seem to love, dialed up to 11 to better make it a grungy harsh Noir place for a classic gumshoe-for-hire to live, and eventually through world hopping the main character does happen to stop by "Mike's Moon" (Heinlein actually names his timelines based on the first man to step foot on the moon, I'm feeling to lazy to look this one up, but this timeline is also prominently known for a Moon AI named "Mike") after the events of "The Moon is a Harsh Mistress" and it reads a lot more Socialist and lot kinder than the protagonist's Moon the book started in.
A lot of modern Libertarians wouldn't expect a crossover boundary with Socialism like Social Credit, which is one of the problems with claiming politics is a spectrum or plane (there are more useful curves where ideologies meet than that), and a lot of modern Libertarians don't trust ideas like UBI and universal health care and don't trust things like unions again this century (despite having past pro-worker/pro-union perspectives), so it is easy to claim that Heinlein shifted over 20 years. But also, if Heinlein was a Social Credit + Libertarian throughout his life, the rest of politics shifted so much around Heinlein that he may have stayed in exactly the same place and it looked like he shifted.
I think his writing certainly shifted, but I think towards cynicism and anger and frustration after WWII and especially after the Red Scare, not necessarily towards deeper Libertarianism.
I also think there are lessons there for modern Libertarians, too. There are modern Libertarians feeling receptive to talking about ideas like UBI again as something that can have space in Libertarian conversations. There could be room in American politics again for a party like Social Credit that can be a coalition between Libertarian values and Democratic Socialist ones. The Libertarians could find better creative allies than destructive tendencies of "the far right". Talking about Heinlein's books isn't a bad place to start those conversations.
Those people were "everyone's self-sufficient, so only a few people down on their luck need the state" folk. They didn't realise that loads of people wouldn't be like that if they didn't have to be.