Here is a snippet of code I am working with:
type Numbers = [3,65,2,7,3,99,23,555];
interface Options {
options?: Numbers;
}
type FilterOption = Options['options'] extends undefined ? undefined : Options['options'][number];
I am trying to define a type that involves referencing the indexes of the "options" property in a union. However, since the `options?` property is optional (indicated by the question mark), I cannot directly assign the value: `Options['options'][number]` as it may be undefined.
I attempted to use a conditional type to check for undefined, but encountered an error when accessing the [number] index:
Type 'Numbers | undefined' has no matching index signature for type 'number'
I considered that this issue might be related to "distributivity", so I tried disabling distribution by adding brackets:
type FilterOption = [Options['options']] extends [undefined] ? undefined : Options['options'][number];
However, that did not resolve the problem. Could this limitation stem from "narrowing"? Is there a viable solution?