I'm trying to create a strong type config which will be used to configure a state machine, where "initial" should be a key of the states property in the same node.
Something like the following:
type StateNode<T extends string = string> = {
initial: keyof T;
states: Record<T, Partial<StateNode>>;
};
function createConfig<T extends StateNode>(config: T): T {
return config;
}
const config = createConfig({
initial: 'alpha', // type should be 'alpha' | 'beta'
states: {
alpha: {
initial: 'x', // type should be 'x' | 'y'
states: {
x: {},
y: {
initial: 'z', // type should be 'z'
states: {
z: {},
},
},
},
},
beta: {},
},
});
Does anyone know how this can be achieved?