Currently, I am in the process of developing a dropdown-style component where the value and options are separate props. My goal is to incorporate Typescript to ensure that the value remains a valid option. Although I could perform this validation at runtime, I believe it should be achievable within TS.
Do you think this is feasible?
interface Props<N extends number> {
count: N,
options: Array<N | number> // Tuples aren't suitable as the length can vary
}
const Component = <N extends number>({count, options} : Props<N>) => {
// While we could do a runtime check to validate `count` against `options`, it would be ideal to handle this earlier if possible
}
Component({count: 5, options: [1,2,3]}) // Ideally, this should return an error