I am looking to limit the parameter that can be passed when executing the next function to only the keys of the object provided in createTransitions. This limitation should be applicable across all transitions, as each transition may have different keys.
type Context = { [key: string]: any };
type Action<T extends { [K in keyof T]: any }> = (
context: Context,
next: (state?: keyof T) => void,
) => Promise<any> | any;
type Transitions<T extends { [K in keyof T]: any }> = {
[K in keyof T]: Action<T>;
};
export function createTransitions<T extends { [K in keyof T]: Action<T> }>(
transitions: T,
): Transitions<T> {
return transitions;
}
const transitions = createTransitions({
trans1: (context) => {
context.payload = context.request.json();
},
trans2: (context, next) => {
if () {
next("trans3"); // I want this to be valid
return;
}
next("error"); // I want this to be valid
},
trans3: (context) => {
next("any"); // I want this to be invalid
},
error: (context) => {
//
},
});
Find the complete code here: Codesandbox