I have the createAction function from ngrx.io Store... with that signature: https://v11.ngrx.io/api/store/createAction
createAction<T extends string, C extends Creator>(type: T, config?: C | { _as: "props"; }): ActionCreator<T>
and this:
createAction(type: T, config: ActionCreatorProps<P> & NotAllowedCheck<P>): ActionCreator<T, (props: P & NotAllowedCheck<P>) => P & TypedAction<T>>
Creator typpe itself has that signature:
type Creator<P extends any[] = any[], R extends object = object> = FunctionWithParametersType<P, R>;
I really have problems reading this, e.g what means config?: C | { _as: "props";}
is it an optional config property with type C or object { _as: "props";} ?
Can someone help me debunk one of those above syntaxes? And btw, the documentations are generally also written in the same language, is that right? so a doc about flutter would have its flutter signatures for methods, etc. right?
This is how I read it.
createAction
: the name of the function type<T, C>
: this makes it a generic function, meaning you could define a function that "implements" this type with two specific parameter types, for example myCreateAction<string, object>T extends string
: the T
type must extend stringC extends Creator
: the C
type must extend the Creator
typeSo this means I can't define my myCreateAction
with string
and object
because object
doesn't extend Creator
. This is a way of limiting what you can pass into an implementation of createAction
.
Let's continue.
(type: T
: the first parameter in the function is named type
and must of of type T
(which had to extend string
, remember?)config?: C | { _as: "props"; })
: the second parameter is named config
, is optional (?
means it can be null) and must be of type C
(which must extend Creator
) or it can be an object
with a _as
property whose value is "props"
. This last piece is NgRx specific and such an object can be created with the props function.: ActionCreator<T>
: the createAction
function must return an object of type ActionCreator<T>
(where T
has to extend string
, as previously defined).So basically, what's happening is we're defining the signature of a function: its name, acceptable parameters and return type.
The same goes for the second function you mention:
T
: the first parameter is of type T
, which can be anything.config
: the second parameter will follow the signature of ActioncreatorProps
and NotAllowedCheck
, both generic again.ActionCreator
, again generic. The ActionCreator
will return an object that follows the signature of P
and TypedAction<T>
.So, let's say you use this createAction
with T
being a string
and P
being a MyAction
(not sure if this is realistic, I have no NgRx experience). The createAction
call would return an ActionCreator<string, (props: MyAction & NotAllowedCheck<MyAction>)
. This ActionCreator
will in turn return a MyAction & TypedAction<string>
object.
Finally, the last statement works the same way:
P
should be an array of whichever typeR
should extend object
FunctionWithParametersType
meaning Creator
is FunctionWithParametersType
except that it limits what P
and R
can be. In this case, it's limited to FunctionWithParametersType<any[], object>
. Defining it separately makes it easier to use and allows limiting which parameters are passed.