Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with generateFunc and TS #245

Open
jaclas opened this issue Jan 31, 2024 · 2 comments
Open

Issue with generateFunc and TS #245

jaclas opened this issue Jan 31, 2024 · 2 comments
Labels
support Questions, discussions, and general support

Comments

@jaclas
Copy link

jaclas commented Jan 31, 2024

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'.
@jaclas jaclas added the support Questions, discussions, and general support label Jan 31, 2024
@kanongil
Copy link
Contributor

Isn't this just because your MyCacheId does not include the id: string property? It is required to use this property for the key id.

@jaclas
Copy link
Author

jaclas commented Jan 31, 2024

@kanongil I have completed the descriprion with the MyCacheId type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

2 participants