Skip to content

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

Closed
@taozhou-glean

Description

@taozhou-glean

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

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