Skip to content

keyof FilteringMappedType can't compute its constraint #56239

New issue

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

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

Already on GitHub? Sign in to your account

Open
Andarist opened this issue Oct 28, 2023 · 3 comments Β· May be fixed by #60528
Open

keyof FilteringMappedType can't compute its constraint #56239

Andarist opened this issue Oct 28, 2023 · 3 comments Β· May be fixed by #60528
Labels
Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Milestone

Comments

@Andarist
Copy link
Contributor

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

@RyanCavanaugh RyanCavanaugh added the Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases label Oct 30, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Oct 30, 2023
@StreetStrider
Copy link

I think I've found this as well (2025, [email protected])

To narrow down:
https://www.typescriptlang.org/play/?#code/FAFwngDgpgBASjAvPKBjA9gJwCYB4DOImAlgHYDmANDAIalgB8oksAKgIxIwDWUY6AM3jBm0GAHEoIANJ8YuWWBhQAHiCils+GIRIUGXRaLYAmLpJl9cvfkLhNjMVgGYuAbxgBtGIphkefILwMAC6AFwSUorWgXYGAL4i4GIWcGhY2PJpqMpqGlooGDgERGRUtPQMBsge3r7+NkHZoREW0YoJjqwALOZS2Rm49sBAA

type R = Record<string, any>
type T1 = keyof R

type GetKey <Key extends string> = Key
type T2 = GetKey<keyof R>

type T3 = { [ Key in keyof R ]: GetKey<keyof R> }

type GetRecord <Rec extends Record<string, any>> = { [ Key in keyof Rec ]: GetKey<Key> }
type T4 = GetRecord<R>

where T1, T2 and T3 compute, while GetRecord not. T4 also compute, but it is based on GetRecord, so error would be present.

Type 'Key' does not satisfy the constraint 'string'.
  Type 'keyof Rec' is not assignable to type 'string'.
    Type 'string | number | symbol' is not assignable to type 'string'.
      Type 'number' is not assignable to type 'string'.

@Andarist
Copy link
Contributor Author

Your case is working as expected. The constraint describes minimal requirement for the type - this is a valid type argument for your GetRecord: Record<PropertyKey, any>. So, as you can see, internally GetRecord can't assume that Key will be assignable to string

@StreetStrider
Copy link

It seemes you are right. I've double-checked and Record creates somewhat open type, so
type T5 = Record<number, any> extends Record<string, any> ? true : false is actually true. Object can be indexed by both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Possible Improvement The current behavior isn't wrong, but it's possible to see that it might be better in some cases
Projects
None yet
3 participants