microsoft / microsoft/TypeScript
Access base class protected methods in static block
まだ誰も着手していません。
- 主要言語
- Go
- スター
- 111k
- フォーク
- 14.3k
- 平均マージ
- 2日 4時間
- マージ済み PR(30日)
- 132
説明
### 🔍 Search Terms
- Dynamic method override
- Static block super
- Static block prototype
### ✅ Viability Checklist
- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [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, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
### ⭐ Suggestion
Hey there, I have a use case where I want to declare/override a protected method using it's prototype.
With the addition of static blocks I can now override the method itself, however I can't call the same method of the base class using super. This makes it impossible to create dynamic overrides of a prototype method atm.
My suggestion would be to allow the usage of the base type prototypes in a static block of the class as well.
This would make it a lot easier to dynamically override/declare a method without using instance fields.
### 📃 Motivating Example
TypeScript now supports dynamic method overrides using static initialization blocks instead of runtime checks.
### 💻 Use Cases
**1. What do you want to use this for?**
Dynamic method declarations / overrides
Dynamic class creation using an options object.
**2. What shortcomings exist with current approaches?**
Currently the only way I see to do this is either:
- Use as any/ which destroys type information
- Use as DerivedType which allows the call with type information
- Always override the method and call the additional method in an if branch which usually requires an instance field
- Declare the method on an instance field instead of the prototype which uses more memory
- Use @ts-ignore because you know what you are doing
**3. What workarounds are you using in the meantime?**
- I use (Base.prototype as DerivedType).myMethod which imho is the best solution atm
**Example:**
https://www.typescriptlang.org/play/?ssl=25&ssc=2&pln=1&pc=1#code/MYGwhgzhAECyCeBhcVoG8BQ1oAcBOA9gC4CmwpAJtALbywlEAWBFAFAJTpbbQD0v0AHTCIBaiWggCAcwCWwbgF8MyjKEgwEAERJ5ZANxIVkG6CQAepAHYVNSFBEzZ8xMpWgFDePRQm16TCwcXDx8AsKC0KLi0GAUFLJEsgRWYCCSMvLc2BAArji6gv4MzGzsShjcEERgScAhPEyyEIIuRMTwBUV0JSzQALzQAGa5VuTJVqxNEABccPA6eobGDpxOoWFCwlFiEnEJSSlpGXIK2Tx5BXjdAaXB-FH5utCjvup4RtBEEABMAGx-AAMlQ28xMUFahHaRE6JBuvQogmAaRAU0YzXYAG5NlZiLEQFIAO6fb4-AAsZL+II2rAQ4JabQ6BViMDAVng7HhgURyIJaIxmwAKuiYISCHgANYwABGuSI0GaLysEDAQxI52wtPsGkhxCZezsiwMRnpnOK3KRKP5EE4D2FirFkplcoVMFGKrV52U2GUiiAA
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
リンクされた TypeScript Playground の例と、static blocks、protected methods、prototype overrides についての issue の議論から始めてください。提案されている base-prototype access がどのように型チェックされるかを追ってください。動作が明確に定義され、関連する型チェックのケースがカバーされていれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript, typescript
- 領域
- compilers
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100