Looking to create a flexible type that can transform existing types into ones that align with a specific API response structure. My goal is to take a variety of types and generate new types based on them, constructed as follows:
// original type definition
interface IModel {
key1: string
key2: string
}
// desired inferred type
interface IModelResponse {
model: {
key1: string
key2: string
}
}
// seeking to achieve this using TypeScript generics
// example implementation:
type IModelResponse = IBaseResponse<IModel, 'model'>
My attempt so far has been:
export interface IBaseResponse<T, modelName extends string> {
[key: modelName]: T
}
However, I encountered the error message "An index signature parameter type must be either 'string' or 'number'." Am I missing something here, or is it not feasible to implement this as intended? Any assistance would be greatly appreciated!