microsoft / microsoft/TypeScript
Private function parameters
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 1日 19時間
- マージ済み PR(30日)
- 117
説明
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.
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
まず、提案されているプライベートパラメーターの構文と、その動機となった再帰関数の例を確認します。記載されている2つの回避策を比較し、自己呼び出しと外部呼び出しそれぞれに必要な型チェック規則を決定します。完了とは、設計が確定し、TypeScriptの型システムおよびJavaScriptの出力への影響が確立されている状態を指します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100