"12-speed" describes how many sprockets are on the cassette. It is not a compatibility standard, and treating it as one is the most common source of a drivetrain that shifts badly and cannot be adjusted into working.
Three things have to match, and the speed count is not one of them.

1. Actuation ratio — shifter to derailleur
Every cable-operated shifter pulls a specific amount of cable per click, and every derailleur moves a specific distance per unit of cable pulled. The two have to correspond.
Manufacturers use different ratios, and they change them between generations of their own products. A shifter from one system driving a derailleur from another will be close enough to work in the middle of the cassette and progressively wrong toward the ends — which presents as a drivetrain that can be indexed for some gears but never for all of them.
Electronic systems remove the cable entirely and replace this with a protocol question, which is stricter rather than looser: they simply do not talk to each other.
2. Freehub standard — cassette to wheel
The cassette has to fit the freehub body on the hub. Common standards are not interchangeable, and the differences are small enough to be confusing.
A cassette with very small sprockets needs a freehub designed to carry them, because a 10-tooth sprocket has a smaller inside diameter than a traditional freehub body. This is why some wide-range cassettes require a specific hub and cannot simply be fitted to an existing wheel.
Check what freehub the wheel has before buying a cassette. Retrofitting is possible on many hubs by changing the freehub body; on others it is not possible at all.
3. Chain width — chain to cassette
Sprocket spacing narrows as the count rises, and the chain narrows with it. A chain from an 11-speed system on a 12-speed cassette is too wide; it will run, and it will shift poorly and wear the cassette unevenly.
Chains within the same speed count from different manufacturers are often close enough to work but are not always designed to. Where the drivetrain uses a specific chain profile — narrow-wide chainrings, or a chain designed to work with a particular cassette geometry — using another brand's chain gives up some of the shifting quality that was designed in.
What usually is interchangeable
Chainrings, subject to bolt pattern and to whether the drivetrain expects a specific tooth profile.
Cranksets, subject to bottom bracket standard and chainline.
Brakes, which are an entirely separate system on a modern bike.
Cassettes within one manufacturer's ecosystem, provided the freehub and the derailleur capacity allow it.
The two numbers to check before buying
Maximum sprocket size the derailleur is rated for. Exceeding it means the derailleur cannot get the chain onto the largest sprocket, or does so with the cage fouling.
Total capacity, which is the difference between largest and smallest sprocket plus the difference between chainrings. This matters on 2x systems and is often the constraint that rules out a wide cassette.
Both are published for every derailleur. Checking them takes a minute and prevents the most expensive class of mistake in a drivetrain upgrade.
Mixing across generations of one brand
Compatibility within a manufacturer is not guaranteed across generations either. A drivetrain revised to a new actuation ratio will not index correctly with the previous generation's shifter, even though both carry the same brand and the same speed count.
Manufacturers publish compatibility charts for exactly this reason, and the chart is the authority rather than the speed number. Where a chart says a combination is not supported, it generally means the ratio differs, and no amount of barrel adjustment resolves it.
The practical buying rule
Buy the shifter, derailleur and cassette as a group from one system, and treat the chain as belonging to that group as well. Mixing is possible in specific documented combinations and is a poor default.
Where a budget forces mixing, mix the parts that do not interact — cranks, chainrings, brakes, wheels — and keep the shifting group intact. That is where the compatibility problems live, and everything else is comparatively free.
