microsoft / microsoft/TypeScript
Cannot reassign undefined to the parameter of a function parameter destructuring with default value
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 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
💻 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
argtoundefinedwhen it is a falsy value. In fact,arg ||= undefinedis not needed, justarg = undefinedwill trigger the issue, but in this case, the original value ofargis 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;
}
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo i tre casi nel TypeScript Playground collegato e confrontali con gli esempi equivalenti di variabile locale e parametro presenti nell’issue. Traccia il modo in cui i valori predefiniti influenzano il tipo del parametro destrutturato; il lavoro è completo quando il comportamento è stato risolto in modo coerente con l’aspettativa documentata ed è coperto da un test di regressione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100