2
我有一個函數,它需要一個參數個數,並生成一個包含唯一散列參數的鍵值映射的新對象。來自函數參數的動態Typescript對象屬性
有沒有一種方法讓Typescript能夠從函數的參數中動態地推斷返回對象的鍵?
例,
生成字典CreateActionType功能:
使用createActionType:
interface ActionTypes {
MY_ACTION_1,
MY_ACTION_2
}
const action = createActionType<ActionTypes>("MY_ACTION_1", "MY_ACTION_2");
/*
* action contains { MY_ACTION_1: "MY_ACTION_1/0", MY_ACTION_2: "MY_ACTION_2/1" }
*/
action.MY_ACTION_1; // returns "MY_ACTION_1/0"
我想刪除的重複數據刪除,並調用createActionType像:
const action = createActionType("MY_ACTION_1", "MY_ACTION_2");
action.MY_ACTION_1; // Intellisense will be able to infer the properties
// MY_ACTION_1 and MY_ACTION_2 from action