I am looking to create an interface or type that can restrict the nested object properties based on keys defined in the main interface.
class MyClass implements MyInterface {
prop1: string;
promp2: number;
nestedObj: {
prop1: string; // Allowed as prop1 is a property in MyClass
prop3: number; // Not allowed as prop3 is not a property in MyClass
}
}
I attempted a solution, but it did not yield the desired outcome.
export interface MyInterface {
[key: string]: any;
nestedObj: { [key2 in keyof Omit<MyInterface, 'nestedObj'>]: any };
}