microsoft / microsoft/TypeScript

Distinguish missing and undefined also in arguments

Open
#44,548 9 comments 6 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files or tests. Start by reviewing the optional-parameter examples and current behavior for omitted, undefined, generic, overloaded, default, and rest-parameter calls. Done means the type system can distinguish presence from absence as proposed without changing JavaScript runtime behavior, including the listed arrow-function and default-argument concerns.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.