Skip to content

Generic type self referenceΒ #44672

@juanrgm

Description

@juanrgm

Bug Report

πŸ”Ž Search Terms

  • Generic type self reference
  • Recursive generic type

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "generic type self reference"

⏯ Playground Link

https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABFApgZygHgFCMQFURQA9UwATNRAb1z0QG0BpRGJAaxQE85gCBdAFyIAFHABGAKxTRh+ADSJOXYU0WhIc5vwCUiALwA+ROHZg4AdzABuOgF9shsVJlQ5e2ngBOKKCC9IEtLQtg7YqBginogAhsLOwVCKyurgEHpGNHR4APQ5iEGuwtQO9Ih5StzCAEQx1dnl+RoQwqbmVvbydOLxhdDJ3KmQGcbRufl9bjSl9BXKNeL1ZRXNrWBmlmCd2HY61kA

πŸ’» Code

function test<
  T extends {
    [K in keyof T]: (object: T, key: K, returnValue: ReturnType<T[K]>) => unknown;
  }
>(object: T) {
  return object;
}

test({
  a: (object, key, returnValue) => {
    // object: {}
    // key: "a"
    // returnValue: any
    return 1
  },
  b: (object, key, returnValue) => {
    // object: {}
    // key: "b"
    // returnValue: any
    return true
  },
});

πŸ™ Actual behavior

test({
  a: (object, key, returnValue) => {
    // object: {}
    // key: "a"
    // returnValue: any
    return 1
  },
  b: (object, key, returnValue) => {
    // object: {}
    // key: "b"
    // returnValue: any
    return true
  },
});

πŸ™‚ Expected behavior

test({
  a: (object, key, returnValue) => {
    // object: { a: ..., b: ... }
    // key: "a"
    // returnValue: number
    return 1
  },
  b: (object, key, returnValue) => {
    // object: { a: ..., b: ... }
    // key: "b"
    // returnValue: boolean
    return true
  },
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions