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 — Generic return type (T[K]) is intersection among all properties of T instead of union · Issue #54901 · microsoft/TypeScript

Generic return type (T[K]) is intersection among all properties of T instead of union #54901

New issue
Copy link
New issue
Copy link
Closed
Closed
Generic return type (T[K]) is intersection among all properties of T instead of union#54901
Copy link
Labels
Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug

Description

@taozhou-glean
Tao Zhou (taozhou-glean)
opened on Jul 6, 2023
Issue body actions

Bug Report

🔎 Search Terms

conditionally generic return type, union vs intersection, valueof etc

🕗 Version & Regression Information

  • This changed between versions 3.3.3 and 3.5

⏯ Playground Link

Playground link with relevant code

💻 Code

export interface ConfigA {
  user: string
}

export interface ConfigB {
  [key: string]: number | string | boolean
}

export interface Config {
  A?: ConfigA
  B?: ConfigB
}

export class ConfigClass {
  config: Config = {}
  get(key: keyof Config) {
    return this.config[key]
  }
}

type ValueOf<T> = T[keyof T]

const config = new ConfigClass()

function a<K extends keyof Config>(key: K): Config[K] {
  return config.get(key)
}

🙁 Actual behavior

Type 'ConfigA | ConfigB | undefined' is not assignable to type 'Config[K]'.
Type 'ConfigA' is not assignable to type 'Config[K]'.
Type 'ConfigA' is not assignable to type 'ConfigA & ConfigB'.
Type 'ConfigA' is not assignable to type 'ConfigB'.
Index signature for type 'string' is missing in type 'ConfigA'.

🙂 Expected behavior

I would expect no error of returning config.get(key) within function a

Activity

  1. olivermrose commented on Jul 6, 2023

    @olivermrose
    Oliver Rose (olivermrose)
    on Jul 6, 2023
    More actions

    get needs to be generic; otherwise, the return type is essentially Config[keyof T].

  2. Ryan Cavanaugh (RyanCavanaugh)
    added
    Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug
    on Jul 6, 2023
  3. taozhou-glean commented on Jul 7, 2023

    @taozhou-glean
    Tao Zhou (taozhou-glean)
    on Jul 7, 2023
    Author
    More actions

    get needs to be generic; otherwise, the return type is essentially Config[keyof T].

    thanks! But even with Config[keyof T], should not it be ConfigA | ConfigB instead of ConfigA & ConfigB ?

    The example is a simplified case just for the minimum repro, in reality we are having this issue with a IPC proxy, and the final xx.get is also generic, but I am getting exact same error, maybe I should update the playground with a more realistic case.

  4. fatcerberus commented on Jul 7, 2023

    @fatcerberus
    Bruce Pascoe (fatcerberus)
    on Jul 7, 2023
    More actions

    But even with Config[keyof T], should not it be ConfigA | ConfigB instead of ConfigA & ConfigB ?

    It’s a side effect of #30769.

  5. typescript-bot commented on Jul 9, 2023

    @typescript-bot
    TypeScript Bot (typescript-bot)
    on Jul 9, 2023
    Contributor
    More actions

    This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

  6. TypeScript Bot (typescript-bot)
    closed this as completedon Jul 9, 2023
  7. microsoft
    locked as resolved and limited conversation to collaborators on Oct 22, 2025
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

    Working as IntendedThe behavior described is the intended behavior; this is not a bugThe behavior described is the intended behavior; this is not a bug

    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.