Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

86
Visualizações
Typescript type definition can occur x amount of times in another type

So this is really hard to describe without code so let me show this first:

type SpacingMultiplier = 0 | 0.5 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8;
type SpacingMultiplierStringOrNumber = `${SpacingMultiplier}` | SpacingMultiplier;
type MultiValueSpacingMultiplier =
  | `${SpacingMultiplier} ${SpacingMultiplier} ${SpacingMultiplier} ${SpacingMultiplier}`
  | `${SpacingMultiplier} ${SpacingMultiplier} ${SpacingMultiplier}`
  | `${SpacingMultiplier} ${SpacingMultiplier}`
  | SpacingMultiplierStringOrNumber;

export interface Spacing {
  padding?: MultiValueSpacingMultiplier;
  paddingTop?: SpacingMultiplierStringOrNumber;
  paddingBottom?: SpacingMultiplierStringOrNumber;
  paddingLeft?: SpacingMultiplierStringOrNumber;
  paddingRight?: SpacingMultiplierStringOrNumber;
  margin?: MultiValueSpacingMultiplier;
  marginTop?: SpacingMultiplierStringOrNumber;
  marginBottom?: SpacingMultiplierStringOrNumber;
  marginLeft?: SpacingMultiplierStringOrNumber;
  marginRight?: SpacingMultiplierStringOrNumber;
}

So as you can see, this is really ugly and unreadable. We are trying to create a spacing component in which it's possible to have margin and padding passed as props (or the individual CSS properties like paddingLeft and marginBottom). Now padding could take a number or string from the SpacingMultiplier (it'll multiply it by 0.25rem since we're using a 4px grid) up to a total of 4 arguments (top, right, bottom, left). Is there any better way to write this like in regex you could with {4} or something?

We first define the SpacingMultiplier type. Then we define that this SpacingMultiplier can be either a string or a number and then we define that it could take up to 4 arguments (in case of defining padding or margin). But the way we're doing this now is, in my opinion, ugly and unreadable.

Is there any better and more readable way to properly do this?

Thanks!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda