Skip to content

Unexpected Auto-Completion Behavior in Type InferenceΒ #61247

Closed as not planned
@Syth-1

Description

@Syth-1

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions