microsoft / microsoft/TypeScript

TypeScript fails to detect self-reference issue in "Parameter Properties" of constructors

Aperta
#62,414 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Bug Domain: check: Control Flow Help Wanted
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

### 🔎 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

https://www.typescriptlang.org/play/?target=9&module=1&ts=5.9.2#code/JYWwDg9gTgLgBAbzgKwsAdnAhgZzgfQDM4BfOQqCEOAcnQgBMBTALjCxgAsaBuAKD5YARjhhQsAY3gSANrjwBZAJ4BhOThwAhXE0QC4BuAHojcLsDwB3YDJlwJWAK45dFnI9eYA5gAoAlACEfIb2EOiiUI5S0HA+YFDAAG4cuoQsZkpgTBDERHAAvOR+eiEk+oZesQB0NVhQXjjpERheANoAusUIwSGGUEwwjlCY5jhVhD41VXUNfvylAj0GRgBUxqajcAwQTHj08JbQANYANHCcEJZMiUxQS3AgSgBijpg+aRlZOQTEhYRd9xCEzm9xIKyMfDKAlk8jgADkmJY1LCmAAPGBMdAMRSqdRaHQlQyoDDVWr1RpwZroNqdQm9MycCxVXxTGY4EELKESMKiewFODoRHwxHIjT+fjc8IQGRMKoyCC+CRVYnoHw0LA0M40ISa2gSGh+OZwIA

### 💻 Code

```ts
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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla riproduzione collegata in Playground e dalla gestione da parte del compilatore delle proprietà dei parametri del costruttore, confrontandola con i controlli di autoreferenzialità dei parametri delle funzioni normali. Conferma il malfunzionamento del JavaScript emesso e aggiungi una copertura nel compilatore affinché il caso del costruttore segnali un errore come “Parameter 'f' cannot reference itself.”

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.