Looking at the type definitions of ngrx's ActionCreator, it looks like this:
export declare type ActionCreator<T extends string = string, C extends Creator = Creator> = C & TypedAction<T>;
here TypedAction is defined as:
export interface Action { type: string; }
export declare interface TypedAction<T extends string> extends Action { readonly type: T; }
And finally Creator:
export declare type FunctionWithParametersType<P extends unknown[], R = void> = (...args: P) => R;
export declare type Creator<P extends any[] = any[], R extends object = object> = FunctionWithParametersType<P, R>;
In a simplified form, ActionCreator is defined as an intersection:
C & TypedAction<T>
If C is a function (Creator)
(...args: P) => R;
and TypedAction is defined as
{ type: string; }
How can this be instantiated? :
(...args: P) => R & { type: string; }
I've trouble understanding the concept of an intersection of a function and a type. Could someone please shed some light on it?
More specifically I'm trying to create an instance of ActionCreator without using the 'createAction' function (for learning purposes), but so far with no success. What should the right-hand side in this example look like to make it work?:
const ac: ActionCreator<string, () => ({ b: number, type: string })> = ([]) => ({ b: 1, type: 'abc' });
The error is:
Type '([]: Iterable<any>) => { b: number; type: string; }' is not assignable to type 'ActionCreator<string, () => { b: number; type: string; }>'.
Type '([]: Iterable<any>) => { b: number; type: string; }' is not assignable to type '() => { b: number; type: string; }'.