我想将RedisGetHashJob转换为RedisKey类型,但我发现错误......
我的类型:
export type RedisKey = {
source: string,
destination: string,
type: string,
id: string,
groupId: string,
pid?: string,
};
export type RedisGetHashJob = {
source: string,
destination: string,
type: string,
id: string,
groupId: string,
pid: string,
};
我有两个功能:
1 - function createKey(obj:RedisKey):string
2 - function getHashJob(obj:RedisGetHashJob):承诺
在我的第二个函数中,必须设置pid属性。但是当我试着把它称为:
function getHashJob(obj: RedisGetHashJob): Promise<string> {
const key = createKey(obj);
....
}
我有以下错误:
Error: src/common/lib/redis.js:60
60: const key = createKey(obj);
^^^ object type. This type is incompatible with the expected param type of
15: export function createKey(obj: RedisKey): string {
^^^^^^^^ object type
Property `pid` is incompatible:
8: pid?: string,
^^^^^^ undefined. This type is incompatible with. See: src/types/redis.js:8
26: pid: string,
^^^^^^ string. See: src/types/redis.js:26
你知道一个很好的方法来完成这份工作吗?
此致
答案 0 :(得分:0)
之所以会这样,是因为您可以删除RedisKey
对象中的RedisKey
属性,它仍然是RedisGetHashJob
对象,但不再是createKey
。
如果你的function createKey(obj: RedisKey): string {
delete obj.pid
return 'a';
}
看起来怎么样
createKey
要解决此问题,您可以在将obj传递给function getHashJob(obj: RedisGetHashJob): Promise<string> {
const objCopy = {...obj}
const key = createKey(objCopy);
...
}
之前复制obj:
{{1}}