Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript区分所有对象成员的联合类型_Typescript - Fatal编程技术网

Typescript区分所有对象成员的联合类型

Typescript区分所有对象成员的联合类型,typescript,Typescript,我有这样的想法: export const MapDispatchToProps = { currentDateChanged, attendanceReceived, updateRecord, updateRecords, }; export type ActionsType = typeof currentDateChanged | typeof attendanceReceived | typeof updateRecord | typeof updat

我有这样的想法:

export const MapDispatchToProps = {
    currentDateChanged,
    attendanceReceived,
    updateRecord,
    updateRecords,
};

export type ActionsType = typeof currentDateChanged | typeof attendanceReceived | typeof updateRecord | typeof updateRecords;

有没有一种方法可以在不重复MapDispatchToProps属性的情况下定义有区别的联合类型ActionsType?

我不知道是否有更干净的方法,但从技术上讲是可行的

function f<T>(o: {[key: string]: T}): T {
  return undefined!;
}

const garbage = f(MapDispatchToProps);

export type ActionsType = typeof garbage;
函数f(o:{[key:string]:T}):T{ 返回未定义!; } 常量垃圾=f(MapDispatchToProps); 导出类型ActionsType=垃圾类型; 浓缩(尽我所能):

constgarbage=((o:{[key:string]:T}):T=>未定义!)(MapDispatchToProps);
导出类型ActionsType=垃圾类型;
注意:仅当每个成员(MapDispatchToProps对象中的每个值)具有相同的属性标记(例如,
类型
)且值不重叠时,才会产生有区别的并集。否则,它将只是一个联盟

const garbage = (<T>(o: {[key: string]: T}): T => undefined!)(MapDispatchToProps);

export type ActionsType = typeof garbage;
export type ActionsType = typeof MapDispatchToProps[keyof typeof MapDispatchToProps];