Velidan
Velidan

Reputation: 6004

Typescript. How to define that an object key is required but it could have 2 different naming?

Basically, I'm trying to handle the situation when I have the style object and this object must have either maxHeight or height properties. In this case, if some of those properties had been passed, the other one isn't required anymore. However, I can't find any possible way to do it via TS. I tried to do it in this way however this solution doesn't work, unfortunately

Example:

type Style = {
  ['maxHeight' | 'height': string]: string | number; 
 }

Many thanks for any help or the appropriate documentation link!

Upvotes: 2

Views: 116

Answers (1)

Clashsoft
Clashsoft

Reputation: 11882

You can use a union type:

type Style = 
    | { maxHeight: string | number }
    | { height: string | number }
;

Upvotes: 2

Related Questions