microsoft / microsoft/TypeScript

Allow non-readonly properties to be used in aliased conditional expressions

Offen
#44,972 1 Kommentar 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.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Suggestion

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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

🔍 Search Terms

aliased conditional expressions condition control flow readonly property

⭐ Suggestion

Some beta feedback. I was excited to try out TypeScript 4.4 because it provided control flow analysis of aliased conditions but was disappointed to learn that it only works for readonly properties as mentioned in the implementation PR. I get the justification; unfortunately, I don’t use readonly because I don’t think the weak immutability guarantees which the modifier provides are worth the syntax noise or annoying assignability errors.

📃 Motivating Example

I digress. Limiting control flow analysis to readonly properties makes code like the following not work, which I would argue is somewhat surprising.

interface A {
  prop: string | undefined;
}

declare const a: A;
// this works
if (a.prop !== undefined) {
	const b: string = a.prop;
	console.log(b);
}

const hasProp = a.prop !== undefined;
// this errors
if (hasProp) {
	const b: string = a.prop;
	console.log(b);
}

💻 Use Cases

I just want to write code which can take advantage of aliased property checks without having to use the TypeScript’s readonly modifier.

Maybe we could assume some basic things about JavaScript object properties for the sake of developer experience. For instance, we could assume that non-getter property test aliases are safe to use in control flow analysis so long as the property is not directly reassigned.

In any case, I think adding this caveat to blog posts and all marketing copy describing the feature would be wise, because I will likely not be the last person to be confused by this limitation, and many TypeScript features are only documented in release notes.

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

Es wird keine Implementierungsdatei, kein Test und kein Einstiegspunkt genannt. Beginne mit dem motivierenden TypeScript-Beispiel und der verlinkten Diskussion zur control-flow analysis-Implementierung und ermittle dann die sicheren Regeln für Aliase von nicht schreibgeschützten Eigenschaften; als erledigt gilt die Aufgabe, wenn das Beispiel korrekt eingegrenzt wird, ohne das erzeugte JavaScript zu ändern, und die Einschränkung gegebenenfalls dokumentiert ist.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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