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];