PALO ALTO — Tesla has agreed to lease a big chunk of office space in Palo Alto, a distinct counterpoint to the company’s revelation this week that it would shift its corporate headquarters from Silicon Valley to Texas.

The electric vehicle maker has struck a deal to lease 325,000 square feet at 1501 Page Mill Road in Palo Alto, part of an office complex in Stanford Research Park, according to five sources with knowledge of the deal.

Earlier this week, Elon Musk, Tesla’s chief executive officer, told company shareholders that the maker of electric vehicles would be moving its corporate headquarters to Texas in an exit of the administrative offices from Palo Alto.

The office space that Tesla is leasing in Palo Alto is owned by Hewlett Packard. The land beneath the building is leased by H-P from Stanford University, a customary property arrangement in Stanford Research Park.

Tesla’s lease deal in Palo Alto serves as a reminder that companies are continuing to expand in the Bay Area, even if, in some cases, they move their headquarters out of this region or other parts of California. The transaction was first reported by The Registry real estate news site.

While headquarters defections offer high-profile economic setbacks, the prospects for ongoing corporate expansions remain bright in Silicon Valley, in the view of Phil Mahoney, an executive vice chairman with Newmark, a commercial real estate firm.

“The glass is more than half full,” Mahoney said. “Numerous sizeable companies are expanding right now for 2022 and 2023 occupancy.”

Among the tech companies that have announced departures of their Bay Area corporate headquarters operations: Hewlett Packard Enterprise decided to move its head offices from San Jose to Texas and Oracle agreed to move its headquarters from Redwood City to Texas.

Yet in the Oracle and HPE instances, both tech companies have kept major operations in the Bay Area.

That appears to be the case with Tesla, which will continue to operate its electric vehicle factory in Fremont and is planning to ramp up manufacturing in California, according to Gov. Gavin Newsom.

“Every time you have a company moving its headquarters out of Silicon Valley, they tend to increase their presence here,” said Dave Sandlin, an executive vice president with Colliers, a commercial real estate firm. “Silicon Valley is pretty secure for now.”