hapijs / hapijs/catbox

Issue with generateFunc and TS

Open
#245 2 comments 0 reactions 0 assignees View on GitHub
support
Dominant language
JavaScript
Stars
494
Forks
74
PR merge metrics
No merged PRs in 30d

Description

### 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.

```js
type MyCacheId = {
id: string;
datasetID: DSType;
params?: ParamsObject;
};

//my generateFunc method
const generateCache = (id: MyCacheId): Promise => {
const datasetID: DSType = id.datasetID;
const params: ParamsObject | undefined = id.params;

return getDataFromDB(datasetID, params);
};

[...]
// registering cache in server
const options: Hapi.CachePolicyOptions = {
segment: DSType[datasetID],
expiresIn: expiresInMs,
generateTimeout: 2000,
generateFunc: generateCache <--- error in this line (158)
};
const cache = server.cache(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' is not assignable to type 'GenerateFunc'.
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'.
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.