microsoft / microsoft/TypeScript

Cannot reassign undefined to the parameter of a function parameter destructuring with default value

Open
#60,279 1 comment 0 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

🔎 Search Terms

"function parameter destructuring", "destructured parameter", "named parameter", "undefined"

🕗 Version & Regression Information
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Why Method Bivariance?"
⏯ Playground Link

https://www.typescriptlang.org/play/?target=0&ts=5.7.0-dev.20241018#code/GYVwdgxgLglg9mABGOAnAtgQwDYAVOqboCmUxqAYuNPGABQEDmAXIgEZxzbGZIA+icABNiwGGGJDEAXkRRUIYgEpEAbwBQiLYiaI+fWcNHjJAbnUBfdetCRYCRCIDO8kNBCpJ+QiTKVq9vSqOqiMMnIKxIgWrMFMrBxcPPyCYCJiElIWKhoAkLr6hmnGmeZWNgG0jsQuCu6eQgDyAA6BON5EpORUdrR0caHhrlExaiGMAPwJnNy80Tnq+YOFqekmQmXWtjQOzq71XgSdfj07YACM-eNDkTpO7DPJeqslktGx49NJcwJGGW-ZNSLAoGF7-DaWLaVXY1fZQDyHHxdfy9BAAJiuulGAxYD2+KT+62i4RxrGG8yBSzCK0JpUhQA

💻 Code
function normalParameterFunction(arg: boolean | undefined = true) {
    arg ||= undefined;
}

function destructuredParameterFunction({ arg = true }: { arg: boolean | undefined }) {
	arg ||= undefined;
}

function destructuredOptionalParameterFunction({ arg = true }: { arg?: boolean }) {
	arg ||= undefined;
}
🙁 Actual behavior

The normalParameterFunction works fine, but in destructuredParameterFunction and destructuredOptionalParameterFunction, I cannot assign undefined to arg.

Type 'undefined' is not assignable to type 'boolean'.

🙂 Expected behavior

I know that if I specify the default value to the arg, the arg will no longer be undefined, but maybe I can reassign it to the undefined.

I guess it should have the same logic as the following:

var arg: boolean | undefined = true;
arg ||= undefined;
Additional information about the issue
  • I don't know if it's really a bug or if it's a bad habit to change the value of a function parameter, but the function without parameter destructuring work as expected. Also, I hate recreate a new variable and come up with a new name in order to change the value of a function parameter, even though assigning the parameter itself directly doesn't modify the variable outside the function.

  • The above code is a real-world use case that aims to set arg to undefined when it is a falsy value. In fact, arg ||= undefined is not needed, just arg = undefined will trigger the issue, but in this case, the original value of arg is ignored and has no practical meaning.

  • You can also pass the type check in disguise with the following code:

function destructuredParameterFunction1({ arg = true as boolean | undefined }: { arg: boolean | undefined }) {
	arg ||= undefined;
}

function destructuredParameterFunction2({ arg }: { arg: boolean | undefined } = { arg: true }) {
	arg ||= undefined;
}

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

Start by reproducing the three cases in the linked TypeScript Playground and compare them with the equivalent local variable and parameter examples in the issue. Trace how default values affect the destructured parameter’s type; done means the behavior is resolved consistently with the documented expectation and covered by a regression test.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.