microsoft / microsoft/TypeScript

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

Ouverte
#62,414 2 commentaires 1 réaction 0 personnes assignées Voir sur GitHub
Bug Domain: check: Control Flow Help Wanted
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

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

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez par la reproduction liée dans Playground et par le traitement par le compilateur des propriétés de paramètres du constructeur, en le comparant aux vérifications d’autoréférence des paramètres de fonction ordinaires. Confirmez l’échec du JavaScript généré et ajoutez une couverture au compilateur afin que le cas du constructeur signale une erreur telle que « Parameter 'f' cannot reference itself. »

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.