Skip to content

keyof FilteringMappedType can't compute its constraintΒ #56239

Open
@Andarist

Description

@Andarist

πŸ”Ž Search Terms

keyof index constraint filtering mapped

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.0-dev.20231027#code/C4TwDgpgBAaghgGwK4QM4B4AqA+KBeKTAbQGsIQB7AM0IF0BuAKEdEigAUAnCgNwEsAJhAEBBAMbAKnfFADejKFFScxALiXBOfAHYBzJooQVdfNVCTaS2igHdtTAL5NGO4BE5U4Y6AGVgcNwBZLwALHQgAYQptKj5dLHFJTlQoCAAPN20BFK5eQWFEqVx5RR0eCjJ1EsUlFXVMQuSiACJlMWaGBSgnRgdmITEEOE5oKgsJPmilCGAkMASJKRT0zOyoACUIMSkBdFRNHV0AGnNLaztsbAAKAH0qrq8k1HuaqCIAaSgdKDJKGgbFslaPVGqgPp1FD0HABKF5QMQjAIQYJiMLaCDqK5dRTbGJxdR+JEotGRaKxeLYmrwZBodDVV6KD5fbQ-cjUQigqBwFKfABkGi0emBckpDNqZneBjFNSMJjMAKe4KlYp6KuwlOwRy60PwuHKgkcTCAA

πŸ’» Code

type Values<T> = T[keyof T];

type ProvidedActor = {
  src: string;
  logic: unknown;
};

interface StateMachineConfig<TActors extends ProvidedActor> {
  invoke: {
    src: TActors["src"];
  };
}

declare function setup<TActors extends Record<string, unknown>>(_: {
  actors: {
    [K in keyof TActors]: TActors[K];
  };
}): {
  createMachine: (
    config: StateMachineConfig<
      Values<{
        [K in keyof TActors as K & string]: {
          src: K;
          logic: TActors[K];
        };
      }>
    >,
  ) => void;
};

πŸ™ Actual behavior

Type 'Values<{ [K in keyof TActors as K & string]: { src: K; logic: TActors[K]; }; }>' does not satisfy the constraint 'ProvidedActor'.
  Types of property 'src' are incompatible.
    Type 'keyof { [K in keyof TActors as K & string]: { src: K; logic: TActors[K]; }; }' is not assignable to type 'string'.
      Type 'string | number | symbol' is not assignable to type 'string'.
        Type 'number' is not assignable to type 'string'.(2344)

πŸ™‚ Expected behavior

It should typecheck

Additional information about the issue

Almost the same thing typechecks OK if we refactor this to [K in keyof TActors & string].

Metadata

Metadata

Assignees

No one assigned

    Labels

    Possible ImprovementThe current behavior isn't wrong, but it's possible to see that it might be better in some cases

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions