I have the following props interface in my component:
export interface ComponentProps {
title: string;
value: number;
}
However, I need to veryfy that this value is a number in range from 0 to 24.
The only way I could find to do that so far is to use a union type:
value: 0 | 1 | 2 ... | 24;
Looks ugly… Is there a more elegant way to do that?
2
Answers
You could make some superior/inferior condition :
Hope I could help !
The technical term for this sort of thing (a number that’s in a specific range) is "dependent types", which is not something that TypeScript attempts to do.
Even if you make a union (either manually or with recursion), it probably won’t be much fun, because TypeScript doesn’t attempt to track the invariants.
You could add simple range validation yourself:
You could enforce this more strictly with a newtype, similar to @Lucas Maracaipe’s suggestion:
Finally, if you really wanted, you could use React’s prop-types. Prop types are almost completely obsoleted by TypeScript, and I wouldn’t recommend them, but their ability to do arbitrary runtime validation is something that TypeScript’s type system can’t do.