Currently, I am utilizing a feature called Once() from FP. In TypeScript, I need to define the types for this function but have been struggling with the implementation. Here's what I have attempted so far:
const once = <T, U>(fn: (arg: T) => U): () => U | undefined => {
let done = false;
return function(this: (args: T) => U) {
return done ? void 0 : ((done = true), fn.apply(this, arguments));
};
}
I am applying this within a class method:
class CSVOutput extends LogOutput {
private print_headers: (logStat: IStats) => void;
constructor() {
super();
this.print_headers = once((logStat: IStats) => {
console.log(Object.keys(logStat).join(','));
});
}
public print(logStat: IStats) {
this.print_headers(logStat);
console.log(Object.values(logStat).join(','));
}
}
In my tsconfig.json
, the configuration appears as follows:
{
"compilerOptions": {
"esModuleInterop": true,
"lib": ["es6", "es7", "es2017"],
"module": "commonjs",
"sourceMap": true,
"target": "es6",
"types": ["node", "mocha"],
"allowJs": true,
"outDir": "./dist",
"preserveSymlinks": true,
"strict": true,
"baseUrl": "."
}
}
A warning in TypeScript is triggered stating
Argument of type 'IArguments' is not assignable to parameter of type '[T]'.
Can someone guide me on correctly defining this function in TypeScript?