镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Description

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

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

  2. taozhou-glean commented on Jul 7, 2023

    @taozhou-glean
    Author

    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.

  3. fatcerberus commented on Jul 7, 2023

    @fatcerberus

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

    It’s a side effect of #30769.

  4. typescript-bot commented on Jul 9, 2023

    @typescript-bot
    Contributor

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

  5. 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 bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions