microsoft / microsoft/TypeScript

Variable aliasing this should be treated the same as this

Offen
#36,618 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Search Terms

this alias

Suggestion

If I do const me = this; then me should be treated the same as this especially since it's a constant and cannot be updated to another value

Use Cases

I'm using --allowJs and --checkJs. Right now TypeScript makes us do this:

	constructor(chart, datasetIndex) {
		this.chart = chart;
		this._ctx = chart.ctx;
		this.index = datasetIndex;
		this._cachedAnimations = {};
		this._cachedDataOpts = {};
		this._cachedMeta = this.getMeta();
		this._type = this._cachedMeta.type;
		this._config = undefined;
		this._parsing = false;
		this._data = undefined;
		this._dataCopy = undefined;
		this._objectData = undefined;
		this._labels = undefined;
		this._scaleStacked = {};

		this.initialize();
	}

Examples

I would like to be able to do this:

	constructor(chart, datasetIndex) {
		const me = this;
		me.chart = chart;
		me._ctx = chart.ctx;
		me.index = datasetIndex;
		me._cachedAnimations = {};
		me._cachedDataOpts = {};
		me._cachedMeta = this.getMeta();
		me._type = this._cachedMeta.type;
		me._config = undefined;
		me._parsing = false;
		me._data = undefined;
		me._dataCopy = undefined;
		me._objectData = undefined;
		me._labels = undefined;
		me._scaleStacked = {};

		me.initialize();
	}

This would make our transition to TypeScript much easier since our code follows this pattern throughout the codebase. It would also result in a smaller file size, which is why we'd adopted this pattern.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Das Issue nennt keine Quelldatei, keinen Test und keinen Einstiegspunkt. Beginne damit, die Beispiele mit --allowJs und --checkJs zu reproduzieren, und untersuche dann, wie ein const-Alias von this derzeit behandelt wird. Als erledigt gilt die Aufgabe, wenn der Alias dasselbe Prüfverhalten wie this erhält, ohne das ausgegebene JavaScript zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
javascript, typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.