microsoft / microsoft/TypeScript

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

Open
#55,383 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: Indexed Access Types Possible Improvement
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 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 https://github.com/microsoft/TypeScript/pull/48837 )

Additional information about the issue

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked TypeScript Playground reproduction and compare the failing generic indexed-access branch with the working variant that adds an array type alias. Read the behavior discussed in PR 48837, then verify the original example no longer reports the constraint error while preserving the expected inferred mapped type behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.