microsoft / microsoft/TypeScript

Private function parameters

Ouverte
#52,811 4 commentaires 3 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Awaiting More Feedback Suggestion
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

Suggestion

🔍 Search Terms

private function parameter

✅ 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.

⭐ 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

function recursive(input: string; private state: State = { … }) {
  // …
  if (logic) {
    return recursive(input, { ...state, extra: true });
  }
  // …
}

// This is okay
recursive('test');

// This is an error
recursive('test', externalState);

// We have to do this now to achieve hiding `state` from the caller
function recursivePublic(input: string) {
  return recursive(input);
}

💻 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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par examiner la syntaxe proposée pour les paramètres privés et l’exemple motivant d’une fonction récursive. Comparez les deux solutions de contournement indiquées et déterminez les règles de vérification des types nécessaires pour les appels récursifs par rapport aux appels externes ; le travail est terminé lorsqu’une conception arrêtée ainsi que son impact sur le système de types de TypeScript et l’émission de JavaScript sont établis.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Fonctionnalité
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.