GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Intersection on indexing a conditional type object · Issue #55804 · microsoft/TypeScript

Intersection on indexing a conditional type object #55804

New issue
Copy link
New issue
Copy link
Closed
Closed
Intersection on indexing a conditional type object#55804
Copy link
Labels
Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed

Description

@MaximilianFullStack
Maximilian (MaximilianFullStack)
opened on Sep 20, 2023
Issue body actions

🔎 Search Terms

"indexing a conditional type object", "type mismatch indexing a conditional type object ts"

🕗 Version & Regression Information

  • This exists on 5.1.6 and latest (5.2.2)

⏯ Playground Link

https://www.typescriptlang.org/play?#code/KYDwDg9gTgLgBAYwgOwM7wNIFECaBlOAXjgG0ByBAQxjIBo4yATCAczoYDMIR2zore3MgF04lVIhToAsACg5oSLElpMuPAH0AKjgAKWDQFkAgrt0BJAHIBxInADecuImoAuBhDAwAllLpO4ZhZ3Pi9fNH9ZZy4QEM8fP1oA-koQqEpkFmBI5240jKzIgF8xCSRVOQVwaHgYAE8wYDgMYDqANUoAGwBXJuJHKLh48NR3dChvTJJhAPTM4Hd7OABbSYB+d2Ru5YAjYCgAbhXKEA24Ld39uCK5G-lZRRq4esa4cxhgZYl+gJIMOEmcAAFC9gBAOM11ABKEgXPZQYTCM4tdpdXokEENMEQ7D4bR6AwmMxWawwjCI26Ve6dYDwDLeZZdUZvD5fOz2KgwdwkABEIB59B5dQFcD5POEdzkXCgwJp8AA1q0hjjoQ4AgB6dVwbrIcLKuCQVCobw7GlwABuaKaoIkgLIKI6PWyATlcBAdnpjM6qBIirqM3uzk1cCwACVQwB5UPuPZIZbACQZAHID5QVDABAJZBwADuAAtgNmYFA6pMWM8IHB07UCxarQFPUzfa1RMQQJTZEA

💻 Code

export const KEYS = ['cat', 'dog', 'fox', 'orca', 'ox'] as const

export const KEYS_TYPE_MAPPING = {
  cat: 'options',
  dog: 'options',
  fox: 'options',
  orca: 'range',
  ox: 'range',
} as const

export type KeyValue = {
  options: string[]
  range: { min?: number; max?: number }
}

export type Items = {
  [K in (typeof KEYS)[number]]?: KeyValue[(typeof KEYS_TYPE_MAPPING)[K]]
}


let animals: Items = {cat: ["x", "y", "x"]}

for (let key of KEYS) {
  // union of possible value types in 'KeyValue'
  let x = animals[key]

  // ERROR: becomes an intersection when trying to set the value
  animals[key] = x
}

🙁 Actual behavior

After creating a object type where each key has a mapped value type, an intersection of said types is created which causes object[key] to not be the same type as object[key] when setting a value at an index of the object.

It should not be an intersection type since the value types never overlap.

🙂 Expected behavior

I expected the type of 'x' (when setting it as the value of object[key]) to be the same type as 'object[key]'. Since they both reference the same index in the same object. Also I would expect the type of 'x' and 'object[key]' to be a union of the value types since they do not overlap.

Additional information about the issue

No response

Activity

  1. fatcerberus commented on Sep 20, 2023

    @fatcerberus
    Bruce Pascoe (fatcerberus)
    on Sep 20, 2023
    More actions

    See #55756 (comment) and #30769. When key is union-typed, as it is here, the compiler doesn’t understand that key refers to the same property in both places. The intersection type arises because that’s the only type that’s known to be valid for any possible key.

  2. MaximilianFullStack commented on Sep 20, 2023

    @MaximilianFullStack
    Maximilian (MaximilianFullStack)
    on Sep 20, 2023
    Author
    More actions

    Interesting, I assume the most optimal work around would be to explicitly type each key value as the union then? Unless there's something else that I'm not aware of.

  3. Andarist commented on Sep 21, 2023

    @Andarist
    Mateusz Burzyński (Andarist)
    on Sep 21, 2023
    Contributor
    More actions

    There might be a way to write this in terms of correlated unions created out of mapped types: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-6.html#indexed-access-inference-improvements

  4. Ryan Cavanaugh (RyanCavanaugh)
    added
    Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed
    on Sep 26, 2023
  5. Ryan Cavanaugh (RyanCavanaugh)
    closed this as completedon Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixedConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.