Saltar al contenido

CódigoSin intentar

Reto de tipos: exactamente una prop flag

Dificultad: difícil

Construye ExclusiveFlags<K> para que <Sign value={n} isPositive /> acepte exactamente una de isPositive, isNegative, isZero: ninguna o dos deben ser un error de compilación. Pista: un miembro de la unión por clave, con las demás claves tipadas ?: never.

Ver solución de referencia
// 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'>;
Cargando editor…