catbox
catbox copied to clipboard
Issue with generateFunc and TS
Runtime
Node
Runtime version
18
Module version
12.1.1
Used with
Hapi Server
Any other relevant information
I use TS 5 and latest catbox-redis
How can we help?
I'm working on moving my Hapi server code to TS for many days now, and I'm stuck on cache migration, which is code that uses Catbox (redis). Specifically, the problem is in registering the generateFunc method with a parameter other than Catbox.Id. I can't get the code to let me explicitly specify the type of the Id parameter, and I want the type because I'm pulling specific data from it in this method.
type MyCacheId = {
id: string;
datasetID: DSType;
params?: ParamsObject;
};
//my generateFunc method
const generateCache = (id: MyCacheId): Promise<DataResultsObject> => {
const datasetID: DSType = id.datasetID;
const params: ParamsObject | undefined = id.params;
return getDataFromDB(datasetID, params);
};
[...]
// registering cache in server
const options: Hapi.CachePolicyOptions<DataResultsObject> = {
segment: DSType[datasetID],
expiresIn: expiresInMs,
generateTimeout: 2000,
generateFunc: generateCache <--- error in this line (158)
};
const cache = server.cache<DataResultsObject>(options);
I get error:
backend | Compilation error in /usr/src/app/src/datasource/cache/cache.layer.core.ts
backend | [ERROR] 10:33:14 ⨯ Unable to compile TypeScript:
backend | src/datasource/cache/cache.layer.core.ts(158,9): error TS2322: Type '(id: MyCacheId) =>
Promise<DataResultsObject>' is not assignable to type 'GenerateFunc<DataResultsObject>'.
backend | Types of parameters 'id' and 'id' are incompatible.
backend | Type 'Id' is not assignable to type 'MyCacheId'.
backend | Type 'string' is not assignable to type 'MyCacheId'.