You need to be rated on both A340 and A330 to fly both. You can't fly an A340 if you just have an A330 on your licence and not A340.
However, your A340 type rating course would be much shorter if you are already type rated on A330.
(And vice versa and it also works with A320.)