microsoft / microsoft/TypeScript
Allow Object.defineProperty() to definitely assign properties
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
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
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:
- https://github.com/microsoft/TypeScript/issues/28694 The same issue, but focused on JavaScript source files.
- https://github.com/microsoft/TypeScript/issues/38115 Might be a duplicate of this issue, but the phrasing of this issue is a little less clear to me based on the examples.
Feel free to close as duplicate!
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo l'esempio collegato di TypeScript Playground e confronta l'errore strictPropertyInitialization riportato con la chiamata a Object.defineProperty del costruttore. Traccia il modo in cui Object.defineProperty e Object.defineProperties vengono gestiti durante il controllo dei tipi e definisci la semantica accettata; il lavoro è completato quando l'esempio motivante viene controllato senza l'errore, mentre il comportamento esistente rimane invariato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- javascript, typescript
- Ambito
- compilers
- Tipo di issue
- Funzionalità
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100