Skip to content

CodeNot attempted

Type challenge: exactly one flag prop

Difficulty: hard

Build ExclusiveFlags<K> so <Sign value={n} isPositive /> accepts exactly one of isPositive, isNegative, isZero: none or two must be a compile error. Hint: one union member per key, with the other keys typed ?: never.

Show reference solution
// Build one object type per key, then index with [K] to turn the map into a union.
// The other keys are `?: never`, so passing a second flag (any value but undefined) is an error.
export type ExclusiveFlags<K extends PropertyKey> = {
[P in K]: { [Q in P]: boolean } & { [Q in Exclude<K, P>]?: never };
}[K];
export type SignProps = { value: number } & ExclusiveFlags<'isPositive' | 'isNegative' | 'isZero'>;
Loading editor…