Constraining types at compile time in TypeScript

Sometimes you want to assert something about your types that can't be expressed via other means, like extends. Here's a little trick:

export type ConstrainType<Constraint, T> = T extends Constraint
    ? any
    : 'type did not meet constraint';

You use it like this:

type A = { a: number, commonField: string }
type B = { b: number, commonField: string }
type C = { c: number }

type AB = A | B;
type AC = A | C;

interface Common {
    commonField: string;
}

const _: ConstrainType<AB,Common> = true;
const _: ConstrainType<AC,Common> = true;