microsoft / microsoft/TypeScript

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

Offen
#60,279 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

🔎 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;
}

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, die drei Fälle im verlinkten TypeScript Playground zu reproduzieren und sie mit den entsprechenden Beispielen für lokale Variablen und Parameter im Issue zu vergleichen. Verfolge, wie Standardwerte den Typ des destrukturierten Parameters beeinflussen; abgeschlossen ist die Aufgabe, wenn das Verhalten konsistent mit der dokumentierten Erwartung gelöst und durch einen Regressionstest abgedeckt ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.