In this discriminated union, the property id
is assigned a different type based on the discriminant value:
type TableKind = 'administration' | 'data'
type AdministrationTableId = 'Modules' | 'Users' | 'Roles'
type DataTableId = number
type GenericIdentifier<K extends TableKind, ID> = { kind: K, id: ID }
type AdministrationTableIdentifier = GenericIdentifier<'administration', AdministrationTableId>
type DataTableIdentifier = GenericIdentifier<'data', DataTableId>
type TableIdentifier = AdministrationTableIdentifier | DataTableIdentifier
A new generic type needs to be defined that can determine the type of the id
property based on the discriminant:
type GetTableIdType<K extends TableKind> = ???
type AdminId = GetTableIdType<'administration'> // returns AdministrationTableID
type DataId = GetTableIdType<'data'> // returns DataTableId
Can such a type be created? How could this be accomplished?