I'm building a dropdown-style component the value and options are distinct props. I'm hoping to use Typescript to ensure that the value is a valid option. While I could do this check at runtime, I feel like this is something I should be able to do in TS.
Is this possible?
interface Props<N extends number> {
count: N,
options: Array<N | number> // Tuples don't work here, since this can be any length
}
const Component = <N extends number>({count, options} : Props<N>) => {
// We could do a runtime check to verify that `count` is in `options`, but I'd rather do the check earlier if possible
}
Component({count: 5, options: [1,2,3]}) // This should be an error
[…O]? I’ve never seen that before