Is it possible to create a type with optional namespaces in TypeScript?
export interface NodesState {
attr1: number;
attr2: number;
attr3: number;
}
The goal is to allow users to namespace the type like this:
{
namespace1: {
attr1: 100,
attr2: 150,
attr3: 200
},
namespace2: {
attr1: 300,
attr2: 400
}
}
But also make it legal to have no namespaces, such as:
{
attr1: 200,
attr2: 100,
attr3: 200
}
I've attempted using this code:
export type MakeState<T> = T & {
[key: string]?: Partial<T>
}
However, TypeScript does not recognize this syntax. Is there a way to achieve what I'm trying to do?