Visitar URL original
Lambda generics are inaccessable inside lambda body Β· Issue #55617 Β· microsoft/TypeScript Β· GitHub
The Wayback Machine - https://web.archive.org/web/20250619225520/https://github.com/microsoft/TypeScript/issues/55617
Skip to content

Lambda generics are inaccessable inside lambda bodyΒ #55617

Closed as not planned
Closed as not planned
@awerlogus

Description

@awerlogus

πŸ”Ž Search Terms

access generic type inside lambda function body

πŸ•— Version & Regression Information

5.3.0-dev.20230831

⏯ Playground Link

No response

πŸ’» Code

const a = <T>(_: T): void => {
  // No error
  type A = T
}

const b: <T>(_: T) => void = _ => {
  // Cannot find name 'T'.ts(2304)
  type A = T
}

πŸ™ Actual behavior

Currently if you define a function as a lambda expression and the type of this lambda is inferred from the type of the variable it's assigned to, attempting to access generic types within the lambda's body results in an error message.

πŸ™‚ Expected behavior

TypeScript to permit the use of generic types within the body of a lambda expression, regardless of whether the lambda's type is explicitly defined or inferred from the variable it's assigned to.

Additional information about the issue

Real world example:

/** @type {<P, R>(set: ReadonlySet<P>, func: Arrow<P, R>) => Set<R>} */
const map = (set, func) => {
  // Have to write Set<ReturnType<typeof func>>
  // But it would be nice to write Set<R> instead
  /** @type {Set<ReturnType<typeof func>>} */
  const result = new Set()

  for (const elem of set) {
    result.add(func(elem))
  }

  return result
}

Quite often, extracting generic types from parameter types may be challenging since it requires deconstruction of the parameter type. Furthermore, the laziness of type inference can frequently make the result type unusable.

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