Closed as not planned
Description
π 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.

