Description
π Search Terms
"auto complete", "type hint" "inference"
π Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about auto-complete not suggesting correctly.
β― Playground Link
π» Code
type State<T extends Record<string, any>, Parent extends any> = {
initial?: keyof T;
states?: NestedStateMachineSchema<T>;
on?: { [K in string]: keyof Parent | undefined };
};
type NestedStateMachineSchema<T extends Record<string, any>> = {
[K in keyof T]: State<T[K], T>;
};
function createTypedMachine<T extends Record<string, any>>(config: State<T, keyof T>) {
return config;
}
const machine = createTypedMachine({
initial: "green",
states: {
green: {
on: {
TIMER: "green",
POWER_OUTAGE: "red",
FORBIDDEN_EVENT: undefined
}
},
yellow: {
on: {
TIMER: "red",
POWER_OUTAGE: "red"
}
},
red: {
on: {
TIMER: "green",
POWER_OUTAGE: "red"
},
}
}
});
π Actual behavior
When the initial state is "green", the on transition values inside green are incorrectly limited to "green". However, for the other states (yellow and red), TypeScript correctly allows all state keys. This behaviour follows with the value that is set within initial
for other values, it shows all suggestions:
π Expected behavior
The suggestions should include all values ('green', 'yellow' and 'red') not just the value that is set to initial
,
Additional information about the issue
Note: This is not a bug with typescript type resolution, if you provide the value 'red' for example, it will not throw an error, but if you provide a value that is not a key in state (eg: 'foo') it will throw an error (this works as it should), this is merely a bug with the options typehint provides.