microsoft / microsoft/TypeScript

Private function parameters

Aperta
#52,811 4 commenti 3 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando la sintassi proposta per i parametri privati e l’esempio motivante di una funzione ricorsiva. Confronta i due workaround elencati e determina le regole di controllo dei tipi necessarie per le chiamate a sé stessa rispetto alle chiamate esterne; il lavoro è completato quando sono stati definiti un design condiviso e il relativo impatto sul sistema di tipi di TypeScript e sull’emissione di JavaScript.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.