Currently, I am in the process of constructing a library and my task involves implementing NestedKeyof
.
During my research, I came across the following code snippet:
type NestedKeyOf<T extends object> = {
[Key in keyof T & (string | number)]: T[Key] extends object
? `${Key}` | `${Key}.${NestedKeyOf<T[Key]>}`
: `${Key}`
}[keyof T & (string | number)];
This piece of code performs as expected.
However, I am currently encountering difficulties with circular references objects
, which is resulting in the following issue:
Type of property 'self' circularly references itself in mapped type '{ [Key in "self" | "name" | "imageManagerIdentifier" | "x"]: Cirulare[Key] extends object ? `${Key}` | `${Key}.${NestedKeyOf<Cirulare[Key]>}` : `${Key}`; }'.(2615)
Provided below is the complete test code:
class Cirulare {
name: string;
self: Cirulare;
public imageManagerIdentifier = 'ImageHandler';
x: number;
constructor() {
(this.name = 'asd'),
(this.self = this);
this.x = 0;
}
}
type NestedKeyOf<T extends object> = {
[Key in keyof T & (string | number)]: T[Key] extends object
? `${Key}` | `${Key}.${NestedKeyOf<T[Key]>}`
: `${Key}`
}[keyof T & (string | number)];
const fn<T> = (keys: NestedKeyOf<T>[])=> {
}
fn<Cirulare>(["name",...])
I am looking for a way to either disable this warning/error or find a simple solution to overcome it. Any suggestions?