2017-06-12 51 views
0

如果我有這個常量,其中的值是所有的功能:打字稿類型包含另一種類型的密鑰,但有不同的值類型

export const staffingReducers = { 
    calendar: calendarReducer, 
    navigation: navigationReducer, 
    shifts: shiftsReducer, 
}; 

我有一個接口定義爲這樣:

export interface StaffingState { 
    calendar: CalendarState; 
    navigation: NavigationState; 
    shifts: ShiftState; 
    selectedId: number; 
} 

我想強制執行,我的人員編制人員與StaffingState具有相同的密鑰。這可能嗎?

回答

3

沒問題:

const staffingReducers: { [K in keyof StaffingState]: Function } = { 
    calendar: calendarReducer, 
    navigation: navigationReducer, 
    shifts: shiftsReducer, 
}; 

根據您當前的代碼會導致一個錯誤,因爲staffingReducers沒有一個屬性selectedId

你可以在這裏找到更多的信息:Mapped Types

+0

這就是我一直在尋找,我要的錯誤!謝謝! –