microsoft / microsoft/TypeScript

Allow setters to return

Aperta
#28,317 4 commenti 6 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

Setters cannot return a value
decorators

Suggestion

By default JavaScript does nothing with the return value of a setter, but it isn't an error.

Allow setters to return as the default behaviour and add flag to turn errors on.

Use Cases

  • Decorators that override the setter and use the original setters return value.
  • Methods that directly call the setter function and expect a return.

Examples

A live example would be @computed from ember-decorators, which matches the old behaviour from ember.

Code example using legacy decorators (same would be possible with stage 2 decorators):

function saved (target: {}, key: string | symbol, descriptor: PropertyDescriptor) {
  const savedStore = new WeakMap();

  const oldSet = descriptor.set;
  if (!oldSet) {
    throw new Error('Must be used on setter only');
  }
  const oldGet = descriptor.get;
  return {
    ...descriptor,
    get () {
      let response = savedStore.get(this);
      if (!response && oldGet) {
        response = oldGet.call(this);
      }
      return response;
    },
    set (value: any) {
      const result = oldSet.call(this, value);
      savedStore.set(this, result);
    }
  };
}

class Foo {
  @saved
  get square () {
    // default value of 1 if none is saved
    return 1;
  }
  set square (value: number) {
    return value * value;
  }
}

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. new expression-level syntax)

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia esaminando il comportamento del setter e del decorator descritto nell’issue, incluso l’esempio di decorator legacy e il suo utilizzo di PropertyDescriptor.set. Confronta il comportamento di ritorno predefinito richiesto con il flag di errore opt-in proposto e con il comportamento esistente di JavaScript. Il lavoro è completato quando il comportamento del linguaggio e la semantica del flag sono specificati e coperti da test appropriati del compilatore.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.