I'm not too sure one correct terms for using TypeScript. But I feel like I'm repeating myself and would like to template my interface better so be less confusing.
I've got a type which is basically a list of potential strings. Then I've used those strings in keys for my interface.
This is my file:
import { IErrorResponse } from '~/interfaces'
export type PRODUCT_ACTION_KEYS =
| 'creatingProducts'
| 'fetchingCategories'
| 'fetchingProduct'
| 'fetchingProducts'
export interface IProductsReducer {
categories: any[]
error: {
creatingProduct?: IErrorResponse
fetchingCategories?: IErrorResponse
fetchingProduct?: IErrorResponse
fetchingProducts?: IErrorResponse
}
is: {
creatingProduct: boolean
fetchingCategories: boolean
fetchingProduct: boolean
fetchingProducts: boolean
}
products: any[]
selectedProduct?: any
}
I'd like to get something like this:
import { IErrorResponse } from '~/interfaces'
export type PRODUCT_ACTION_KEYS =
| 'creatingProducts'
| 'fetchingCategories'
| 'fetchingProduct'
| 'fetchingProducts'
export interface IProductsReducer {
categories: any[]
error: {
[PRODUCT_ACTION_KEYS]?: IErrorResponse
}
is: {
[PRODUCT_ACTION_KEYS]: boolean
}
products: any[]
selectedProduct?: any
}
Would something like this be achievable in TypeScript?
Thanks!