microsoft / microsoft/TypeScript

maintain certain class property characteristics in mapped types, like protected/private visibility, and instance property vs instance function

Aperta
#35,416 11 commenti 10 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

mapped types with protected private members

Suggestion

Feature request:

Allow types/interfaces to contain member visibility as well as property type ("instance member function" vs "instance member property") and perhaps other intrinsics.

Or at least keep these characteristics intact in mapped types where the mapped type is generated from class types.

Use Cases

I've made a function called multiple that allows me to do multiple inheritance. In plain JavaScript, it works fine, like this:

class One {one() {}}
class Two {two() {}}
class Three {three() {}}
class Four extends multiple(One, Two, Three) {
  test() {
    this.one() // OK
    this.two() // OK
    this.three() // OK
  }
}

So far, after making type definitions for multiple(), the above works fine in TypeScript. playground example.

However when making properties protected or private, those properties are lost from the mapped type returned from multiple(), and therefore inheritance of protected (or denial of accessing private) members doesn't work:

class One {protected one() {}}
class Two {protected two() {}}
class Three {private three() {}}
class Four extends multiple(One, Two, Three) {
  test() {
    this.one() // ERROR, Property 'one' does not exist on type 'Four'. Expected no error.
    this.two() // ERROR, Property 'two' does not exist on type 'Four'. Expected no error.
    this.three() // ERROR, Property 'three' does not exist on type 'Four'. Expected an error relating to private access.
  }
}

Here's the playground example.

Because the private properties are deleted from the mapped type (just like the protected ones are), it becomes possible to inadvertently use private properties because the type system says they don't exist:

class One { private foo = 1 }
class Two { }
class Three extends multiple(One, Two) {
	foo = false
	test() {
		this.foo = true // Uh oh! There's no error using the private property!
	}
}

Here's the playground example.

Another problem is methods of the classes passed into multiple() are converted from instance member function to instance member property, and this happens:

class One {one() {}}
class Two {two() {}}
class Three {three() {}}
class Four extends multiple(One, Two, Three) {
	one() {} // ERROR, Class '{ three: () => void; two: () => void; one: () => void; }' defines instance member property 'one', but extended class 'Four' defines it as instance member function.(2425)
}

Here's the playground example.

Related

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code I'm not sure
  • 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

L’issue non indica alcun file o test del repository; inizia riproducendo gli esempi collegati del playground relativi a mapped types, multiple() e alla compatibilità dei membri delle classi. Il lavoro sarebbe concluso quando esistesse un design definito che preservi la visibilità di protected/private e distingua le funzioni membro di istanza dalle proprietà, con diagnostica appropriata per l’accesso a private e gli override.

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.