In my coding project, I've been noticing a pattern of redundancy when it comes to creating TypeScript interfaces as the code base expands.
For example:
interface IErrorResponse {
code: number
message: string
}
// Feature 1
type FEATURE_1_KEYS =
| 'fetchingActivities'
| 'fetchingActivityTypes'
interface IFeature1ErrorAction
extends IErrorResponse {
key: FEATURE_1_KEYS
}
// Feature 2
type FEATURE_2_KEYS =
| 'fetchingSomethingElse'
| 'updatingSomething'
interface IFeature2ErrorAction
extends IErrorResponse {
key: FEATURE_2_KEYS
}
Currently, I find myself using IFeature1ErrorAction
and IFeature2ErrorAction
for the final error actions in my project which consists of multiple features.
Is there a way to streamline this process by creating an IErrorAction
that builds from IErrorResponse
, allowing me to simply pass in the respective FEATURE_KEYS
for the key?
This would enable me to use
IErrorAction<FEATURE_1_KEYS>
like so.
This optimization would greatly reduce repetition during development, although I'm unsure about the exact implementation at this stage.
The desired outcome for the interface
IErrorAction<FEATURE_1_KEYS>
would be as follows:
interface IErrorAction = { // How can I incorporate that `TEMPLATED_KEYS`?
code: number,
key: TEMPLATED_KEYS,
message: string
}