microsoft / microsoft/TypeScript

Suggestion: Add readonly references to objects & mark readonlyThis methods (pure functions)

Aperta
#42,758 0 commenti 1 reazione 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

Suggestion

🔍 Search Terms

  • readonly method
  • readonly this
  • sealed class
  • sealed this
  • readonly reference #17181 (this issue is simmilar but more restrictive)

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code.
    • It can or cannot be. It depends on wich name we choose
  • 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

Have a way to check readonly object references (including parameters, and this parameter). That allows to check than a function does not modifys an object.

📃 Motivating Example


class Figure{
    constructor (public name: string){}
    checkName(){
        this.name += ' !'
    }
    readonlyMethod logAccess(){
        console.log('one instance of Figure was accesed');
    }
    readonlyMethod safeCheck(){
       // @ts-expect-error
        this.name += ' x'  // this is readonly in a readonlyMethod context
    }
}

function showRO(figure: readonlyReference Figure){
    console.log(figure.name);
    // @ts-expect-error
    figure.name+=" x";  // if figure is a readonly reference you can use it to modify the object
    // @ts-expect-error
    figure.checkName();  // if figure is a readonly reference you can use it to modify the object calling a method
    figure.logAccess();  // ok logAccess is a readonly method
}

var x = new Figure('triangle');

showRO(x);

var z: readonlyReference Figure = x; // ok, you can assign a writeable object to a readonlyReference

// @ts-expect-error
var y: Figure = z ;  // you cannot do the oposite. 

💻 Use Cases

Actually I'am ussing Readonly aproach, but it not contemplates all cases.

play example

I am trying with Object.seal and Proxyies .

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

La issue non indica file di implementazione né test. Inizia esaminando la proposta, la issue collegata #17181, gli obiettivi di progettazione di TypeScript e l'esempio Playground collegato. Il lavoro sarebbe completato quando fosse disponibile un design concordato e implementabile per riferimenti e metodi readonly, insieme ai test del compilatore identificati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
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.