Skip to content

Generic indexed access on a substitution type with an array constraint resolves incorrectly #55383

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 Aug 15, 2023 · 0 comments Β· May be fixed by #55386
Open

Generic indexed access on a substitution type with an array constraint resolves incorrectly #55383

Andarist opened this issue Aug 15, 2023 · 0 comments Β· May be fixed by #55386
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

indexed access substitution type array constraint

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play?#code/C4TwDgpgBAMglsCAnAhgGwCrmgXigZ2CTgDsBzKAHyhIFcBbAI2SqkYHt20IUSBuAFChIUAGrpaEAOIRgiJAB5xaSVhEQAHohIATfLATJ0a3AfnHsAPih4AFAEob15auyCh2KAGUAxgAsIehQbb39AlABBJFQQVl8AoIB5RgArCB9gVnhzTDcPEXjwqJiQ4pQQBUKk1PTM6irI6PKsw1RcyEtBYWgG5LSMkIBvASgoAG0AawgQAC4CIlIyAF053pqB+rCgstjqbKN2iEEAX3duqABJEgAzZBcIfAAxJHZ6BsqoTW09UISUazwXk+Wggun0+zaJhGUAA-GIJNJZPJKpZoatgd99A0dtC4cNRgTxgBpKCkKBTEDsa7eFaXG53BFPF5vLYoSpjIlLTrQ0bHNHeDGgn5rfrAXFQfGE4mkkjk6ZUmlzK63JD3Jmvd5eDlcwSEvmjOYkCAAN2QgiAA

πŸ’» Code

type LiteralType = string | number | boolean;
type ValueGetter<ValueType extends LiteralType = LiteralType> = () => ValueType;

type Schema = SchemaArray | SchemaObject | LiteralType;
type SchemaArray = Array<SchemaObject | SchemaArray | LiteralType>;
type SchemaObject = {
  [key: string]: SchemaObject | SchemaArray | LiteralType;
};

type InferValuesFromSchema<S extends Schema> = S extends LiteralType
  ? ValueGetter<S>
  : S extends SchemaArray
  ? {
      [K in keyof S]: InferValuesFromSchema<S[K]>;
    }
  : S extends SchemaObject
  ? {
      [K in keyof S]: InferValuesFromSchema<S[K]>;
    }
  : never;

πŸ™ Actual behavior

an error is reported:

Type 'S[K]' does not satisfy the constraint 'Schema'.
  Type 'S[keyof S]' is not assignable to type 'Schema'.
    Type 'S[string] | S[number] | S[symbol]' is not assignable to type 'Schema'.
      Type 'S[string]' is not assignable to type 'Schema'.
        Type 'S[string]' is not assignable to type 'SchemaObject'.
          Type 'S[keyof S]' is not assignable to type 'SchemaObject'.
            Type 'S[K]' is not assignable to type 'SchemaObject'.
              Type 'S[keyof S]' is not assignable to type 'SchemaObject'.
                Type 'S[string] | S[number] | S[symbol]' is not assignable to type 'SchemaObject'.
                  Type 'S[string]' is not assignable to type 'SchemaObject'.(2344)

πŸ™‚ Expected behavior

There should be no error, S[K] should satisfy Schema constraint (at least since #48837 )

Additional information about the issue

A variant with an additional type alias for that array branch works OK: TS playground.

@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 Aug 15, 2023
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 15, 2023
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
2 participants