microsoft / microsoft/TypeScript

Allow configuration of missing property quickfix

Offen
#32,296 0 Kommentare 1 Reaktion 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

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.

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

Beginnen Sie mit src/services/codefixes/fixAddMissingMember.ts um die Zeilen 204-210, der in der Issue identifizierten Implementierung. Ermitteln Sie, wo der Quickfix für fehlende Member die Sichtbarkeit der Property auswählt und wie eine Konfigurationsoption dies steuern könnte, einschließlich des Anwendungsfalls für private Felder. Als erledigt gilt die Aufgabe, wenn der Quickfix so konfiguriert werden kann, dass er eine private Property generiert, während das bestehende Standardverhalten erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
developer-experience, tooling
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.