将参数传递给const

时间:2019-02-08 15:38:56

标签: typescript

对于打字稿而言,我是一个新手,我有一个快速的问题,我似乎无法弄清楚。

我正在尝试使用传递给函数的两个参数来帮助我从对象获取值

export type Ontology = 'Ren' | 'Covea'
export type Client = 'GMF' | 'default'
export type TabEnum =  Array<LabelledEnum<string>>
export type MapTabRestClientEnum = Record<Client, TabEnum>
export type MapTabRestOntEnum = Record<Ontology, MapTabRestClientEnum>

export function getTabEnumByMandant<K extends Ontology, T extends Client> 
(ontology : K, client: T) {
    return TypeRendezVousValuesMap[ontology].[client]
}

export const TypeRendezVousValuesCovea = (<T extends MapTabRestClientEnum>( 
o: T) => o)({
    GMF : fromNameLabels([
        ["RdvGalant","Rendez vous galant"],
        ["RdvSport","rendez vous sportif"],
    ]),
    default : fromNameLabels([
        ["RdvSport","rendez vous sportif"],
    ]) 
}) 

export const TypeRendezVousValuesRen= (<T extends MapTabRestClientEnum>(o: 
T) => o)({
    GMF : fromNameLabels([
        ["Site","Sur site"],
        ["Visio","En visio"],
        ["Telephonique","Par téléphone"],
        ["RdvGalant","Rendez vous galant"],
        ["RdvSport","rendez vous sportif"],
        ["MatchSquash","match de squash"],
    ]),
    default : fromNameLabels([
        ["Telephonique","Par téléphone"],
        ["RdvGalant","Rendez vous galant"],
        ["RdvSport","rendez vous sportif"],
        ["MatchSquash","match de squash"],
    ]) 
})

export function fromNameLabels<T extends string>(src: Array<[T, string]>): 
Array<LabelledEnum<T>> {
    return src.map(([name, label]) => ({ name, label }))
}

export 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))
}


export const TypeRendezVousValuesMap= (<T extends MapTabRestOntEnum>(o: T) 
=> o) ({
    'Ren': TypeRendezVousValuesRen,
    'Covea': TypeRendezVousValuesCovea
})

getTabEnumByMandant('Covea','GMF')

我想做的是这样,但是不要把Ren和GMF放参数了

export function getTabEnumByMandant<K extends Ontology, T extends Client> 
(ontology : K, client: T) {
    return TypeRendezVousValuesMap.Ren.GMF
}

当我使用参数时,不是给我正确的答案,而是总是给我这个答案:

const TypeRendezVous2: {
    Site: "Site";
    Visio: "Visio";
    Telephonique: "Telephonique";
    RdvGalant: "RdvGalant";
    RdvSport: "RdvSport";
    MatchSquash: "MatchSquash";
}

0 个答案:

没有答案