-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
Bug Report
π Search Terms
default destructuring scope parameter argument ReferenceError
π Version & Regression Information
- This is the behavior in every version I tried
β― Playground Link
Playground link with relevant code
π» Code
function foo({a, ...x}, b = a) {
return b
}
console.log(foo({a: 1}))
π Actual behavior
If you run TypeScript's output, you get ReferenceError: a is not defined
. This is the problematic output:
"use strict";
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
function foo(_a, b = a) {
var { a } = _a, x = __rest(_a, ["a"]);
return b;
}
console.log(foo({ a: 1 }));
Notice how b = a
is evaluated before { a } = _a
.
π Expected behavior
If you run the JavaScript directly you should get 1
, so TypeScript's output should do that too.
Additional context is here: evanw/esbuild#1826. I got a report about this bug in esbuild and it turns out TypeScript has the same bug, so I'm filing it here too. My apologies if this is a duplicate (I couldn't find one that had already been filed).
RyanCavanaugh, pspeter3, Beraliv, caenguidanos, DannyFeliz and 3 morewhzx5byb
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue