microsoft / microsoft/TypeScript
TypeScript fails to detect self-reference issue in "Parameter Properties" of constructors
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 2 T. 4 Std.
- Gemergte PRs (30 T.)
- 132
Beschreibung
🔎 Search Terms
- private property self reference
- private property self reference constructor
- Parameter Properties constructor self reference
🕗 Version & Regression Information
- This is reproducible at least between v4.9.5 and nightly -- I have not tried earlier versions
⏯ Playground Link
💻 Code
import { join as _f } from 'node:path';
abstract class MyClassBase {
// this will cause issue in g()!
constructor (private f: typeof _f = f) {
}
g (...args: string[]) {
return this.f(...args);
}
/* // this does not work, however
myFun (f: typeof _f = f) {
f();
}*/
}
class NewClass extends MyClassBase {
join (...args: string[]) {
this.g(...args);
}
}
const c = new NewClass();
console.log(c.join('a', 'b', 'c'));
🙁 Actual behavior
TypeScript compiler does not report any errors.
if you copy transpiled JS file and run that with Node.js, you will see
constructor(f = f) {
^
ReferenceError: Cannot access 'f' before initialization
🙂 Expected behavior
TypeScript clearly shows an error, similar to what would have been seen in regular function parameters
Parameter 'f' cannot reference itself.
Additional information about the issue
The issue originated from this -- https://github.com/microsoft/vscode-js-debug-browsers/issues/19, and I just discovered that the issue in the project or TypeScript has never been fixed. If someone at Microsoft could do a favor and ping the maintainer of that repo as well, it will be much appreciated.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginnen Sie mit der verlinkten Playground-Reproduktion und der Behandlung von Konstruktor-Parameter-Properties durch den Compiler und vergleichen Sie diese mit den Prüfungen auf Selbstreferenzen in regulären Funktionsparametern. Bestätigen Sie den Fehler im erzeugten JavaScript und fügen Sie Compiler-Tests hinzu, damit der Konstruktorfall einen Fehler wie „Parameter 'f' cannot reference itself.“ meldet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 35/100