One alternative approach to achieve reusable typing could involve establishing a relationship between keys and types, followed by creating a union type PaneMap
that outlines the expected value type for each key within Panel
:
type Panel = "store" | "logs"
/*
Establish a reusable type relationship
*/
type PanelRelation<K extends Panel, V> = { [key in K] : V }
/*
Define a union type that specifies the relationship between keys from Panel and
their corresponding value types. This ensures that each key is correctly mapped
to a specific value type
*/
type PanelMap =
PanelRelation<'store', StoreComponent> &
PanelRelation<'logs', LogsComponent>
/* Example of how it can be used */
type LogsComponent = string
type StoreComponent = number
const object:PanelMap = {
"store": 1,
"logs": "x",
// "logs": 1, <-- this would lead to a type mismatch error as expected
// "foo": 1, <-- foo key not part of Panel leads to an expected error
}