Closed DuShuYuan closed 8 months ago
This is a bogus error. The super constructor is in fact only called once. Sometimes you cannot easily define parameters to inherited methods using the nullish coalescing operator or similar constructs, particularly in method overload situations.
This is a bogus error. The super constructor is in fact only called once. Sometimes you cannot easily define parameters to inherited methods using the nullish coalescing operator or similar constructs, particularly in method overload situations.
Please how can I fix this error, my project is not running now
I can try to debug that if you give me a complete example (as a repo or full code here), which reproduces the issue. As I wrote above, IMO it's not an error.
Thanks, I've solved it, I modified the constructor. But I don't understand why a different constructor is called depending on the argument, why not just call super(...).
Strange that this is accepted by your tsc. It should not! Keep in mind this is a method overloading situation. The two overloads either allow no parameters at all or exactly two. If you use invokingStateNumber
directly you violate this requirement, because this parameter can be unassigned, leading to the situation that only one parameter is given.
Are you directly transpiling with tsc
? Do you have an automatic syntax checker in your project (like VS Code has)?
Cause: Modify "target": "ES2021" in tsconfig.json
Strange that this is accepted by your tsc. It should not! Keep in mind this is a method overloading situation. The two overloads either allow no parameters at all or exactly two. If you use
invokingStateNumber
directly you violate this requirement, because this parameter can be unassigned, leading to the situation that only one parameter is given.Are you directly transpiling with
tsc
? Do you have an automatic syntax checker in your project (like VS Code has)?
Thanks, I see. But my project has to support ES2021, or even ES2015, and it seems that this syntax is not available anymore
You can always bundle your code to support older ES versions. The bundler will then generate wrappers or polyfills from unsupported code.
I think we see the same error. The weird thing, it happens only in a production build, where the code is transpiled by Webpack.
Here's the excerpt of the transpiled code (which it seems corresponds to InterpreterRuleContext
:
function PV(e, t) {
if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object")
}
function IV(e, t, n) {
PV(e, t), t.set(e, n)
}
function LV(e, t, n) {
return function(e, t, n) {
if (t.set) t.set.call(e, n);
else {
if (!t.writable) throw new TypeError("attempted to set read only private field");
t.value = n
}
}
Xre = (Iee = new WeakMap, class extends jte {
constructor(e, t, n) {
void 0 !== n ? (super(t, n), IV(this, Iee, {
writable: !0,
value: void 0
})) : (super(), super(t, n), IV(this, Iee, {
writable: !0,
value: void 0
})), LV(this, Iee, e)
}
get ruleIndex() {
return kV(this, Iee)
}
})
Notice the super(), super(t, n)
We use the ESM version of the library, which is distributed as
var Pt = class extends q {
#e;
constructor(e, t, r) {
r !== void 0 ? super(t, r) : super(), this.#e = e
}
get ruleIndex() {
return this.#e
}
}
Oh, so it's a webpack problem actually? Thanks for the analysis @nashbridges. I guess I need to find a different way then to implement this.
Right. Sorry for being confusing. This is either webpack or, more likely, babel's plugin issue. I haven't yet checked if upgrading them would help. Another potential fix is to bump up browserlist, so that private properties would not have to be transpiled, but that's not an option for many.
Another potential fix is to bump up browserlist
I can confirm that setting "last 2 years" in the browserlist makes the issue disappear.
@nashbridges I'd like to use the suggested values from the NPM docs. Would that also work?
Yes, setting "defaults and fully supports es6-module" also helps with the issue.
The location of the error:
I guess the corresponding code position should be:
I see a similar code in antlr4-c3 as well.
It's mine tsconfig.json:
tsconfig.worker.json:
I think it can be solved this way: