Define a type AnyFn that represents any function:
export type AnyFn = (. args:any[]) = > any;
let a: AnyFn;
a = (data) = > console.log(data);
a('Jerry');
a = 1; // Syntax error
Copy the code
The code above assigns 1 to a, which causes a compilation error because 1 is of type number.
Define a MemoizedProjection type:
export type MemoizedProjection = {
memoized: AnyFn;
reset: () = > void;
setResult: (result? :any) = > void;
clearResult: () = > void;
};
Copy the code
Define a variable of type MemoizedProjection:
let a: MemoizedProjection;
let b = (data) = > console.log(data);
a.memoized = b;
a.reset = () = > console.log('reset');
a.setResult = (result) = > console.log(result);
a.clearResult = () = > console.log('clear');
Copy the code
The above code execution will report an error:
Uncaught TypeError: Cannot set property ‘memoized’ of undefined:
In line 29 below, the T in Angle brackets cannot be replaced by any. Any cannot appear in the position of type parameters. If representing any type that does not occur in the position of a type parameter, use any.
Generic type ‘MemoizedSelector<State, Result, ProjectorFn>’ requires between 2 and 3 type arguments.
Add method to interface:
Here’s an example from Spartacus: A function has four attributes that refer to four other functions in addition to its own implementation:
The projector of memoizedProjector is completed:
Constructed by the following method:
Object.assign. This method merges the attributes of each Object of the input parameter:
More of Jerry’s original articles can be found in “Wang Zixi” :