Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Unexpected Auto-Completion Behavior in Type Inference #61247

Open
Syth-1 opened this issue Feb 22, 2025 · 2 comments Β· May be fixed by #56182
Open

Unexpected Auto-Completion Behavior in Type Inference #61247

Syth-1 opened this issue Feb 22, 2025 · 2 comments Β· May be fixed by #56182
Labels
Duplicate An existing issue was already created

Comments

@Syth-1
Copy link

Syth-1 commented Feb 22, 2025

πŸ”Ž 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

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAysCGwIB4AqUIA8kDsAmAzlAEoQDGA9gE57IHBUCWOA5gDRTw4gB8HACvCoQcwDNhGFO3HlAC8UAN4AoKFGaNgjeABsA-AC4oAawggKAMyioA3MtVR6iCAUNQAci6R44zgLLwZAAWzBAwwRAAtvBoPHYOFDhuilAA2gDS6jiODMwsALpGpuZWgsKiUAA+UACu+BAWoXhQAL52bfagkB5eED4ISAHBoeFBUTHoWLhSpJQ0dLmsHFy8sgoqahlZJmaW1oWwAyioGfkcqHHKHcoWdWRaiVBkws6o4H1DITjH4tNEs9RaPQmEtpKsABSUHCNFhGXxINAcYp7C4ASiUDmEwBqVGyUJh7Xsyih9Cg0WG33kTxeSDekDwn1C4I2WU02h0RgARCxhCJOWwHE4kAQjCy1DyICJRQ41GpEtLZYrrABJPwAUWIXIlfIFStl-AA8gB1DUAfQNAFVUABBADiaq5wjw-JlSoAYgbiAAhZUAEV9avcprVADVA6gjHU8A0mq61C1XS1dbKQBAdDoKAB3BVK+UYvVqVCqjWOvougtQQ0m4jmq12h1QTlOzlx1qJ5NqJ05xV5sVKovqzWN7U4csFqtmy02+2l52tpOJhwJlqouxAA

πŸ’» 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

Image

for other values, it shows all suggestions:

Image

πŸ™‚ 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.

@jcalz
Copy link
Contributor

jcalz commented Feb 22, 2025

It's weird; it looks like the issue is inside states[k] when initial is k. So if you make it "red" then the red object has the issue (which is at least less insane than something specific to the string literal "green"). Anyway, barring a change or fix, I'd probably work around this by making initial's type NoInfer<keyof T> or something.;

@Andarist
Copy link
Contributor

A fix for this is already pending for quite a bit here: #56182 . You can even see almost the same example of this behavior in one of the test cases I have added there, see here. I have also explained there why this happens (implementation-wise).

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 24, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants