Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!