Repository: TypeScript
Issue: maintain certain class property characteristics in mapped types, like protected/private visibility, and instance property vs instance function
Labels: Suggestion,Awaiting More Feedback

## 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:

```js
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](http://www.typescriptlang.org/play/#code/PQ18ZXTt3BYAUCABAWQK4BsAuBLAB2wFNV8A7ACxICd9cBDCgYzPwFtiSOSKmCAewoAaVLgCehEgGdUw7BNQAzQbVQAjWiQBuDCQC5U8RCmCoAKjRU5s5LqV79GQiqgDuDKuQoN8jOxkSXHllcVlcOXw5F1QqXFxCGQMQAHMvTA0AOhZBDmBcWkwZAGtC4GxBdxZsRhkZZGQAExIaxm0bVldUDhwCbgAeC1QSAA9cPia5AGFhGULMFlw1AG0AXQA+AAos3bb62SMLAEojWY4NShIm6dqDmSGNxqRKCdplRjZUWYp5oqW1EMRuNJjEKEoALyoADeAF8NjDkABICgkdw7XbtVLJVDMCTrU6WZEURi8Ix-SipZCw56SaSoADKVHwylwQLGEwoU1x4PWCKhWwxWSxOJOqAhCLxx2BnO5WwA+kY8WJdsLaNijJRlHRUAAlaUSnlKAD8etQRlROjoyDpZHQ+FG1wAgrRaIwJOyQVyZnMFgDaAM8Rs+eLUPL7Y6mi63R6LGI4U8kLawxHna73Z7ZT7fn7lgGg+sxABVfmIpBIgAMFrRqC2BoRReRAEYjOGHWmYwNkUimSy2RYNiJu9Du0iVgBpHyoEokCSCMIASRzzDYFikJCGKwrm1QAB9p7P56gi2sjJOOaCD3OwkXUKaixO1ubUEv5iuSGvpJvt8Hx2tuzS5aJrCKwWCsADkpAUKkuBUOBT4Xt6qAVneyHPk2-5IDa67fHklyojcdxBA8A6hsMiHcusyKmraR4APIaAAVq0uDIkcMqXiseKYUippgdubGWBxSE-H8ix5lR5amqmUbprGiZIkYmBciQyhXE0zwmFp2k6bpMDIGgYyktwxh6WZ5m6cg+xyHRqIwsIJB1jCsKAdZljuIIMK4B5Tlwq5RGWFQ2hkNCsHBb5LlWQFABigiYOoFFyL0eBEKQWy2SQYgWB5WVBSQJDSiOSCoOE8y+cgJUlbB0RZA5TloHR44VZV1UyFk3mCPV5iNc1VXMm1YX5V1qA9cVqCAbCQA).

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:

```ts
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](http://www.typescriptlang.org/play/#code/PQ18ZXTt3BYAUCABAWQK4BsAuBLAB2wFNV8A7ACxICd9cBDCgYzPwFtiSOSKmCAewoAaVLgCehEgGdUw7BNQAzQbVQAjWiQBuDCQC5U8RCmCoAKjRU5s5LqV79GQiqgDuDKuQoN8jOxkSXHllcVlcOXw5F1QqXFxCGQMQAHMvTA0AOhZBDmBcWkwZAGtC4GxBdxZsRhkZZGQAExIaxm0bVldUDhwCbgAeC1QSAA9cPia5AGFhGULMFlw1AG0AXQA+AAos3bb62SMLAEojWY4NShIm6dqDmSGNxqRKCdplRjZUWYp5oqW1EMRuNJjEKEoALyoADeAF8NjDkABICgkdw7XbtVLJVDMCTrU6WZEURi8Ix-SipZCw56SaSoADKVHwylwQLGEwoU1x4PWCKhWwxWSxOJOqAhCLxx2BnO5WwA+kY8WJdsLaNijJRlHRUAAlaUSnlKAD8etQRlROjoyDpZHQ+FG1wAgrRaIwJOyQVyZnMFgDaAM8Rs+eLUPL7Y6mi63R6LGI4U8kLawxHna73Z7ZT7fn7lgGg+sxABVfmIpBIgAMFrRqC2BoRReRAEYjOGHWmYwNkUimSy2RYNiJu9Du0iVgBpHyoEokCSCMIASRzzDYFikJCGKwrm1QAB9p7P56gi2sjJOOaCD3OwkXUKaixO1ubUEv5iuSGvpJvt8Hx2tuzS5aJrCKwWCsADkpAUKkuBUOBT4Xt6qAVneyHPk2-5IDa67fHklyojcdxBA8A6hsMiHcusyKmraR4APIaAAVq0uDIkcMqXiseKYUippgdubGWBxSE-H8ix5lR5amqmUbprGiZIkYmBciQyhXE0zwmFp2k6bpMDIGgYyktwxh6WZ5m6c8+xyHRqIwoQtCCBMSzXPIqJ1jCsKAdZljuII9mOc5ExNOIfkeXC3lEZYVDaGQ0IOfgOguGQsGxeFXnID5ABigiYOoFFyL0eBEKQWy2SQYgWH5lUxSQJDSiOSCoOE8zhcgzXNbB0RZMIJAeWgACiuq6nRupiAACo50i0JIqDgb14GoE0giyKgFBOcC0QhMI4g4eBOV5eBWSoANozSC5IXrSMrpqFk7UdV1MhZLgYXSoNw2jRNU10LN4EvYIi3Lat60hGMW1ubt9L7bltBHSdZ0sa5V10I5tB3U1D3Mk9qV1f15hDSNY2oJNgjTb9OMkIDK1yCDm3zBDybQ4dx2nedwU8tdqOoNotQENB4j+QlSUTLiLBsPU6PNYBsJAA).

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:

```ts
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](http://www.typescriptlang.org/play/?ssl=64&ssc=2&pln=43&pc=1#code/PQ18ZXTt3BYAUCABAWQK4BsAuBLAB2wFNV8A7ACxICd9cBDCgYzPwFtiSOSKmCAewoAaVLgCehEgGdUw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VO41xZABk3Upp5idfth2IydSBnRddkBddLkqJDD2BeWwVUG94WoJ9MMAKK6rqdG6mIqR3U0E0Az9TbdjluDA6OiMQ-d4hFBV5h8wLQuoCLghiwT5ZTUAA).

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

```ts
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](http://www.typescriptlang.org/play/#code/PQ18ZXTt3BYAUCABAWQK4BsAuBLAB2wFNV8A7ACxICd9cBDCgYzPwFtiSOSKmCAewoAaVLgCehEgGdUw7BNQAzQbVQAjWiQBuDCQC5U8RCmCoAKjRU5s5LqV79GQiqgDuDKuQoN8jOxkSXHllcVlcOXw5F1QqXFxCGQMQAHMvTA0AOhZBDmBcWkwZAGtC4GxBdxZsRhkZZGQAExIaxm0bVldUDhwCbgAeC1QSAA9cPia5AGFhGULMFlw1AG0AXQA+AAos3bb62SMLAEojWY4NShIm6dqDmSGNxqRKCdplRjZUWYp5oqW1EMRuNJjEKEoALyoADeAF8NjDkABICgkdw7XbtVLJVDMCTrU6WZEURi8Ix-SipZCw56SaSoADKVHwylwQLGEwoU1x4PWCKhWwxWSxOJOqAhCLxx2BnO5WwA+kY8WJdsLaNijJRlHRUAAlaUSnlKAD8etQRlROjoyDpZHQ+FG1wAgrRaIwJOyQVyZnMFgDaAM8Rs+eLUPL7Y6mi63R6LGI4U8kLawxHna73Z7ZT7fn7lgGg+sxABVfmIpBIgAMFrRqC2BoRReRAEYjOGHWmYwNkUimSy2RYNiJu9Du0iVgBpHyoEokCSCMIASRzzDYFikJCGKwrm1QAB9p7P56gi2sjJOOaCD3OwkXUKaixO1ubUEv5iuSGvpJvt8Hx2tuzS5aJrCKwWCsADkpAUKkuBUOBT4Xt6qAVneyHPk2-5IDa67fHklyojcdxBA8A6hsMiHcusyKmraR4APIaAAVq0uDIkcMqXiseKYUippgdubGWBxSE-H8ix5lR5amqmUbprGiZIkYmBciQyhXE0zwmFp2k6bpMDIGgYyktwxh6WZ5m6c8+xyHRqIwsIJB1jCsKAdZljuIIMK4B5Tlwq5RGWFQ2hkNCsHBb5LnIG5ABigiYOoFFyL0eBEKQWy2SQYgWB5WVBSQJDSiO5YORFpmoAAorqup0bqYi3HUcjgdC4h5SQRhOYaOiCPgTQANziB57X1qgXU9f1DlDeKCKjX1qCwuBqAtGpqJRMurBkLwFw6oQtCCNItCSKg4EOeBYgaJgIQUdcqBueBsXxQtS1XFEIR1D4b7rT0PAaDqyjKUs+DCFkWwAEwACwgwArMc1JAA).

## Related

- https://github.com/microsoft/TypeScript/issues/26760
- https://github.com/microsoft/TypeScript/issues/7124
- https://github.com/microsoft/TypeScript/issues/3854

## Checklist

My suggestion meets these guidelines:

* [ ] This wouldn't be a breaking change in existing TypeScript/JavaScript code **I'm not sure**
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).