I have this code:
export interface StoreCursor {
created: Date;
id: number;
}
export default class PageUtil {
public static decode<T>(cursor?: string, dates?: (keyof T)[]): T | undefined {
if (!cursor) {
return;
}
const json = Buffer.from(cursor, "base64").toString("utf8");
return JSON.parse(json, (key, value) => (dates?.includes(key) ? new Date(value) : value));
}
}
const decoded = PageUtil.decode<StoreCursor>(cursor, ["created"]);
But I get next error, and I don't know how solve it:
error TS2345: Argument of type 'string' is not assignable to parameter of type 'keyof T'.
I've also tried:
public static decode<T>(cursor?: string, dates?: (keyof T & string)[]): T | undefined {
[...]
}
But the error is similar:
error TS2345: Argument of type 'string' is not assignable to parameter of type 'keyof T & string'. Type 'string' is not assignable to type 'keyof T'.
I want dates to be limited to keys of StoreCursor (or better, only to keys of StoreCursor of type Date).
StoreCursor(createdorid) not to their values. I also have triedexport interface TattooStoreCursor extends Record<string, unknown> { [...] }but the error is the same. - xeladejo