microsoft / microsoft/TypeScript

Allow configuration of missing property quickfix

Aperta
#32,296 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

Search Terms

property does not exist quickfix
quick fix
create private property
quickfix configuration

Suggestion

When referencing a property that doesn't exist within a class, you get the error Property 'foo' does not exist on type 'MyClass'.ts(2339). In VS Code you can then do cmd-. to get the quickfix option Declare property 'foo'. When you select this, a property is declared, but it is created with default visibility.

Based on digging in to the code here:

https://github.com/microsoft/TypeScript/blob/15daf42b2c0bc4f06970c9e4688f1a93bff0f7a0/src/services/codefixes/fixAddMissingMember.ts#L204-L210
it doesn't look like there's anyway to configure this.

It would be really nice if there was a way to configure the quickfix functionality to default to creating properties with a given access modifier (in my case I'd like to default them to private). Given the upcoming #field private-named fields stuff, it seems like this could also be a useful way to let people decide what they want to use.

Use Cases

I would like to have the properties created by quickfix all start as private, to encourage writing classes with strong encapsulation.
The current approach requires me to go back and modify the generated declaration every time.

Examples

Starting code with missing property:

export class MyClass {
  public constructor(foo: number) {
    this.foo = foo;
  }
}

After applying the quickfix to the line this.foo = foo; it becomes:

export class MyClass {

  foo: number;

  public constructor(foo: number) {
    this.foo = foo;
  }
}

but I would like some way to configure it to instead do:

export class MyClass {

  private foo: number;

  public constructor(foo: number) {
    this.foo = foo;
  }
}

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

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 da src/services/codefixes/fixAddMissingMember.ts intorno alle righe 204-210, l’implementazione identificata nell’issue. Determina dove il quickfix per il membro mancante sceglie la visibilità della proprietà e come un’opzione di configurazione potrebbe controllarla, incluso il caso d’uso di un campo privato. Il lavoro è completo quando il quickfix può essere configurato per generare una proprietà privata preservando il comportamento predefinito esistente.

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

Valutazione

Stack tecnologico
typescript
Ambito
developer-experience, tooling
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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.