Typescript NgRx createAction方法的含义';s返回类型签名

Typescript NgRx createAction方法的含义';s返回类型签名,typescript,generics,ngrx,Typescript,Generics,Ngrx,我已经对createAction方法进行了NgRx测试,如下链接所示: 我无法理解此方法的以下类型签名,特别是createAction方法的返回类型: 什么是 () => TypedAction<T> ()=>TypedAction 在本签名中: ActionCreator<T, () => TypedAction<T>> ActionCreator类型操作> 我没有看到任何关于TypedAction?它是指任何特定动作类型形状的物体吗

我已经对createAction方法进行了NgRx测试,如下链接所示:

我无法理解此方法的以下类型签名,特别是createAction方法的返回类型: 什么是

() => TypedAction<T>
()=>TypedAction
在本签名中:

 ActionCreator<T, () => TypedAction<T>>
ActionCreator类型操作>
我没有看到任何关于
TypedAction
?它是指任何特定动作类型形状的物体吗

在返回类型的上述签名中,我对T的理解是,它是ActionCreator函数的泛型类型,调用时将返回T类型的操作。 但不确定另一个类型参数是否指示,除了它似乎是某个返回T类型的
TypedAction
的函数。
想知道一个真实的例子。

TypedAction
是一个通用接口,它通过添加一个只读的
type
属性来扩展操作类型

export declare interface TypedAction<T extends string> extends Action {
  readonly type: T;
}
createAction
函数在action\u creator.ts中定义

export function createAction<T extends string>(
  type: T
): ActionCreator<T, () => TypedAction<T>>;
定义类型为:

function defineType(type: string, creator: Creator): Creator {
  return Object.defineProperty(creator, 'type', {
    value: type,
    writable: false,
  });
}
defineType
接受类型(“[Auth]注销”)和创建者-
()=>({type})
。它返回Creator,但带有一个新属性
type
。 因此调用
logout.type
logout().type
将返回相同的值-“[Auth]logout”

稍后,在reducer_creator.ts中,它允许我们(“[Auth]Logout”),将其与reducer函数关联,并

更新:
随着这个问题的答案越来越大,我决定写一篇博文

谢谢你的回答。另一个附带的查询是-在我们使用泛型类型的地方使用工厂函数表示法是Typescript语法吗?我在文档中没有找到任何具体的例子,他们使用工厂函数作为泛型类型。使用T是直观的,因为它指示ActionCreator函数将接受的类型参数。然而,使用另一个参数会混淆需要两种类型,因为第一种是输入参数,第二种是输出
()=>TypedAction
。再次查看createAction声明
导出函数createAction
它有一个输入参数
type:T
,输出为
Creator
。ActionCreator的输出参数是否正确?为什么要添加额外的参数?()=>TypedAction是ActionCreator函数的输出参数?据我所知,createAction是一个函数,它接受T作为输入参数,并返回T类型的ActionCreator。但是为什么()=>TypedAction被添加到ActionCreator?这有点不正确。createAction本身属于ActionCreator类型(而不是返回值)。createAction接受输入字符串并输出()=>TypedAction以精确表示。
export function createAction<T extends string, C extends Creator>(
  type: T,
  config?: { _as: 'props' } | C
): Creator {
...
    case 'empty':
      return defineType(type, () => ({ type }));
...
}
function defineType(type: string, creator: Creator): Creator {
  return Object.defineProperty(creator, 'type', {
    value: type,
    writable: false,
  });
}