I'm struggling to figure out how to search for this specific question on Stack Overflow. The structure of my generic type FetchOptions
looks like this:
type FetchOptions<T> = {
explode?: string & keyof T | (string & keyof T)[];
}
I'm trying to find a way to define another type string & keyof T
without repeating it in the subsequent array definition, all while avoiding extracting the type separately within its own type:
type Key<T> = string & keyof T;
type Options<T> = {
explode?: Key<T> | Key<T>[];
}
Here's an example of how it could be used:
class Product {
id: number | string;
name: string;
variants?: ProductVariant[];
attributes?: ProductVariant[];
}
const fetchProducts = (options: FetchOptions<Product> = {}) => {
// ...
};
fetchProducts({ explode: 'variants' });
fetchProducts({ explode: ['variants', 'attributes'] });