But the private keys of the segwit addresses (both P2SH and bech32) are the same as those of a legacy address when you export a private key from a segwit address in Bitcoin Core and import into Electrum, it appears that you imported a legacy address instead of segwit P2SH or bech32). Do the tests yourself
That is a known problem. Fixing it requires a new private key format because the private keys do not actually specify what kind of address you should create with them. It is important to note that private keys only create public keys, and a public key can be transformed into several different addresses now. The private key does not specify what kind of address to create from the public key.
What prevents wallet software from spending from non-segwit addresses using the segwit address corresponding to a common public key?