I've encountered an issue with my function while filtering a labelled enumeration. It seems that the function isn't returning the expected type, and I'm not sure why.
function fromNameLabels<T extends string>(src: Array<[T, string]>):
Array<LabelledEnum<T>> {
return src.map(([name, label]) => ({ name, label }))
}
function fromLabelledEnum<T extends string>(src:
Array<LabelledEnum<T>>): { [K in T]: K } {
return src.reduce((res, {name, label}) => {
res[name] = name
return res
}, Object.create(null))
}
type Mandant = 'REN' | 'COVEA'
type TabEnum = Array<LabelledEnum<string>>
type MapTabEnum = Record<Mandant,TabEnum>
function getTabEnumByMandant ( mandant: Mandant) {
return TypeRendezVousValuesMap[mandant]
}
export const TypeRendezVousValuesREN = fromNameLabels([
["Site","Sur site"],
["Visio","En visio"],
["Telephonique","Par téléphone"],
["RdvGalant","Rendez vous galant"],
["RdvSport","rendez vous sportif"],
["MatchSquash","match de squash"],
])
export const TypeRendezVousValuesCOVEA = fromNameLabels([
["RdvGalant","Rendez vous galant"],
["RdvSport","rendez vous sportif"],
["MatchSquash","match de squash"],
])
export const TypeRendezVousValuesMap: MapTabEnum = {
'REN':TypeRendezVousValuesREN,
'COVEA':TypeRendezVousValuesCOVEA
}
export const TypeRendezVousCovea = fromLabelledEnum(getTabEnumByMandant ('COVEA'))
export type TypeRendezVous = keyof typeof TypeRendezVousCovea
I am aiming for my TypeRendezVousCovea to have this specific type:
const TypeReference: {
ReferenceSinistre: "ReferenceSinistre";
NumeroContrat: "NumeroContrat";
Autre: "Autre";
}
However, currently it is of this type :
const TypeRendezVousCovea: {
[x: string]: string;
}
If anyone has any insights or possible solutions, I would greatly appreciate it. I'm using TypeScript version 3.0.1.