Ir para o conteúdo

CódigoNão tentada

Desafio de tipos: exatamente uma prop de flag

Dificuldade: difícil

Construa ExclusiveFlags<K> para que <Sign value={n} isPositive /> aceite exatamente uma entre isPositive, isNegative, isZero: nenhuma ou duas devem ser erro de compilação. Dica: um membro de união por chave, com as outras chaves tipadas como ?: never.

Ver solução de referência
// 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'>;
Carregando editor…