microsoft / microsoft/TypeScript

Distinguish missing and undefined also in arguments

オープン
#44,548 コメント 9 件 リアクション 6 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.4k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

Suggestion

With the arrival of --strictOptionalProperties, it’s clear that the intent of ? as a modifier isn’t to add | undefined to the type but to distinguish presence from absence.

I propose the same should apply to arguments.

🔍 Search Terms

optional argument, optional arguments, optional parameter, optional parameters

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code (unless behind a flag)
  • 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

Given

function optionallyTakesAnArg(arg? : string) {
	return arguments.length;
}

calling optionallyTakesAnArg() is not the same as optionallyTakesAnArg(undefined). TypeScript should be able to model this. Specifically it should forbid the second form and only allow either optionallyTakesAnArg() or optionallyTakesAnArg(stringArg).

📃 Motivating Example

This is mostly an issue when the argument is a generic type that may or may not include an explicit undefined:

function optionallyTakesAnArg<T>(arg? : T) {
	if(arguments.length > 0) {
		// arg passed, do stuff
		doStuffWith(arg!);
	} else {
		// no arg passed, do something else
		doOtherStuff();
	}
}

💻 Use Cases

I want to model that calling optionallyTakesAnArg<string>() and optionallyTakesAnArg<string>(stringArg) both are valid but that optionallyTakesAnArg<string>(undefined) is not, whereas optionallyTakesAnArg<undefined>() and optionallyTakesAnArg<undefined>(undefined) are (but may not do the same thing).

Further, overloads should be able to distinguish between the latter two cases, e.g. for inference of return types.

This allows arguments whose type is a generic that may include undefined to be modeled correctly.

⚡️ Open issues

  • Arrow functions don’t have an arguments object in scope so the only way to implement this would be with rest params and tuples, which means they, too, would need new semantics for ?.
  • This proposal is at odds with default arguments which always treat absent and undefined the same ((function(a = 1) { return a })() and (function(a = 1) { return a })(undefined) both evaluate to 1).
  • It’s also possible to model optional args using optionallyTakesAnArg(arg : string | void), which, AFAICT currently has the same semantics as optionallyTakesAnArg(arg? : string). I’m not sure whether this should continue to be aligned or not.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

この issue ではファイルもテストも指定されていません。まず、オプショナルパラメーターの例と、省略、undefined、ジェネリック、オーバーロード、デフォルト、rest パラメーターを使った呼び出しに対する現在の動作を確認してください。完了の条件は、提示された案どおりに型システムが存在と不在を区別でき、Arrow 関数とデフォルト引数について列挙された懸念を含め、JavaScript の実行時動作を変更しないことです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。