-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Suggestion: Type variables in function signature #32525
Comments
This is really the intended design of type parameters - they are parametric. Any time you're trying to use a defaulted type parameter to introduce a new name, something has gone wrong. For example, you could have written <Obj extends object>(object: Obj): Array<[keyof Obj, Obj[keyof Obj] ]>; to get the desired effect without introducing a second type parameter. It's important that callers be able to specify type parameters at all times so that they can resolve potential ambiguities |
I might have been unclear in my phrasing, forgive me. Type parameters are fine, but their purpose is to parametrize function, not introduce variables. You've pointed me to that in the comment with an example, but I've also included this exact example in the original post. The core of the suggestion is described in the Examples section. Basically, I'm suggesting introducing completely different concept / entity |
Usually for a suggestion we need to start with something that can't be done, or can't be done with adequate fidelity. This doesn't really meet the bar for that. You might also be interested in local type aliases, #23188 |
That really looks a lot like what I'm suggesting, thanks |
Search Terms
type variable variables param parameter parameters
Suggestion
Add type variables in function signatures.
Such type variables would not be visible nor controllable by the caller of the function (sort of like private type parameters).
Use Cases
Currently (v.3.5.1) all the type parameters in the function signatures are visible by the user of this function (the one who calls it). In the following example, we have the unnecessary ability to provide typing for the second type parameter
Key
of the functiongetEntries
:The suggested functionality is achievable though in the current version of TypeScript, but it is highly repetitive - notice the usage of
keyof Obj
type in the following example:Examples
The proposed functionality might introduce
where .. is ..
syntactical construct, more fully described as this:Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: