I am working with an interface and a union type containing "result types":
export interface ResponseData {
products: string[];
content: string[];
}
export type resultTypes = 'categories' | 'products' | 'content' | 'files';
The resultTypes
represent the possible properties of the ResponseData
.
I am looking for a way to indicate that the keys in the definition of ResponseData
can be dynamic, meaning they must align with one or more of the resultTypes
.
I attempted to create another interface that would extend this one, where the keys were dynamic, but unfortunately I was not successful.
In this example, I provide a "typeProp" (of type "resultTypes") to a function expecting a 'keyof ResponseData', resulting in the error:
Argument of type 'resultTypes' is not assignable to parameter of type 'keyof ResponseData'
export interface ResponseData {
products: string[];
content: string[];
}
export type resultTypes = 'categories' | 'products' | 'content' | 'files';
const response: ResponseData = {products: ['r'], content: ['r']}
const currentResultTypes = Object.keys(response) as resultTypes[];
type typeProperty = keyof typeof response;
const hasResultsOfType = (typeProp: typeProperty) => {
return typeProp in response && response[typeProp];
};
const typeProp = currentResultTypes[0];
const isValid = hasResultsOfType(typeProp);