Private function parameters #52811
Labels
Awaiting More Feedback
This means we'd like to hear from more people who would be helped by this feature
Suggestion
An idea for TypeScript
Suggestion
π Search Terms
private function parameter
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Hi, I would like to propose adding private function parameters to TypeScript. Private function parameters can be only provided by the function itself when it calls itself recursively. Providing them when called in a different context is an error.
Private function parameters always come after public function parameters in the function signature.
π Motivating Example
https://twitter.com/TomasHubelbauer/status/1626321532187189248
π» Use Cases
Enables writing recursive functions which need parameters to pass state between calls without having to private the "implementation" function which has the parameter for state and export a different function that has the same signature except for the state parameter(s) and is meant for outside callers.
There are two workarounds I am aware of:
Write another function which is public, make the original one private and export only the public one which doesn't have the state parameter in its sigature
The shortcoming of the this workaround is that we need to introduce another function that is meant to be called by others which passes along the parameters to the implementation function which does the real processing but has the extra parameter(s) which cannot be exposed to non-self callers.
Use function within a function, call the local function when the state parameter is uninitialized and handover to it
This is a special case of the above, we just move the implementation function into the wrapper function.
The text was updated successfully, but these errors were encountered: