microsoft / microsoft/TypeScript

Allow Object.defineProperty() to definitely assign properties

Offen
#42,919 6 Kommentare 11 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

Suggestion

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

⭐ Suggestion

It would be nice if Object.defineProperty() and Object.defineProperties() calls could be read via the constructor to definitely assign class properties.

📃 Motivating Example

https://www.typescriptlang.org/play?ssl=12&ssc=2&pln=1&pc=1#code/MYGwhgzhAEDiBOBTRAXR9oG8BQ0-QDswBbRALmghXgEsCBzAbl33qVTvoqtoef2jAA9gR4BXYCiHwAFEVLdqnAJRYWAvCgAWNCADp5iaAF5CJRPw14A8gCMAVokl6AJogBmdRAAV4QgA7oKACeMtq6ADTQAERsyCic0VE4Vlb0qDKqKak5SChi8ATQAAZaiCAgQtAAJJiGAL7F6jnQ9c0C9cqWeG31QA

class Greeter {
    name: string;
    greeting: string;
    constructor(name: string) {
        this.name = name;
        Object.defineProperty(this, "greeting", {
            get() {
                return `hello ${name}`
            }
        });
    }
}

Currently the greeting property in the class will cause the error Property 'greeting' has no initializer and is not definitely assigned in the constructor.

💻 Use Cases

This is not a big deal because we can use a “definite assignment assertion“ (!) after the greeting property identifier, but it would be nice for TypeScript to be aware of these defineProperty calls.

🔍 Search Terms

constructor, Object.defineProperty, Object.defineProperties, definitely assigned, strictPropertyInitialization, definite assignment operator
Property has no initializer and is not definitely assigned in the constructor.

This is potentially a duplicate of the following issues:

Feel free to close as duplicate!

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

Beginne damit, das verknüpfte TypeScript Playground-Beispiel auszuführen, und vergleiche den gemeldeten strictPropertyInitialization-Fehler mit dem Object.defineProperty-Aufruf im Konstruktor. Verfolge, wie Object.defineProperty und Object.defineProperties während der Typprüfung behandelt werden, und definiere die akzeptierte Semantik; abgeschlossen ist die Arbeit, wenn das motivierende Beispiel ohne den Fehler geprüft wird, während das bestehende Verhalten unverändert bleibt.

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
35/100

Neue Issues direkt in Ihr Postfach

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