Skip to content

typeof generic function with a specific template type should be possible #47542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
brandon942 opened this issue Jan 21, 2022 · 3 comments
Closed
5 tasks done
Labels
Duplicate An existing issue was already created

Comments

@brandon942
Copy link

Suggestion

⭐ Suggestion

function func<T>(a: T) { return a }
var value : ReturnType<typeof func<number>> // should be possible

Related to #37181 but even simpler - have a concrete type instead of a template type.
I wonder how this simple thing is not possible.

💻 Use Cases

function getValue<T>() {
	var a : T
	return { a: a }
}
var value
value = getValue<number>() // assigned later lazily
value // any (no typehint)

Currently it is impossible to have a variable be of a specific type that involves a generic function.

var value : ReturnType<typeof getValue<number>> // Error!

One has to create extra functions as a workaround, just to get a typehint for a variable:

// A workaround
function getValue<T>() {
	var a : T
	return { a: a }
}
function getValue_number() { // Extra function workaround just to avoid template syntax in typeof!
	return getValue<number>()
}
var value : ReturnType<typeof getValue_number>
// ...
value = getValue<number>() // assigned later
value // { a: number } - OK

🔍 Search Terms

typeof generic function

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@brandon942 brandon942 changed the title typeof generic function with a specific template type typeof generic function with a specific template type should be possible Jan 21, 2022
@RyanCavanaugh
Copy link
Member

Duplicate #40542 ? I'm not sure what distinguishes this, #40542, and #37181 - they all seem to be the same thing IMO

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 21, 2022
@brandon942
Copy link
Author

brandon942 commented Jan 21, 2022

I'm not sure what distinguishes this, #40542, and #37181

Probably the expected operator precedence of typeof.
This opened issue focuses on the simpler case for which there is even a workaround. Defining a concrete type from a referenced generic function.
I don't see a workaround for the template type version.

@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants