Is there a way to instruct TypeScript to utilize different implementations of methods within the same class, based on the specified target
option in the tsconfig.json
file?
I am currently transitioning one of my scripts to TypeScript to streamline management of codebases by consolidating an ES5 and an ES6 file into a single source. Due to TypeScript's ability to switch output targets in the tsconfig.json
file, I aim to have a single version that can be easily updated and maintained.
The challenge arises from using a generator in the ES6 version, which should not pose a problem as TypeScript automatically adds a pseudo-generator to the top of the ES5 output. However, my current Pseudo-Generator implementation in the ES5 file is more concise and clean.
Possible Solutions
Is there a method to override the respective "generator" method or use specialized comment annotations (such as //@ts-target
) to specify which code (function body) to use based on the selected target
in the configuration file, even if it is not documented officially?
Integrating an additional function or script into the TypeScript compiler process may provide assistance since I compile both ES files through a small node.js script without directly altering the tsconfig.json
file.
Alternatively, is there an extension available to segregate different methods of a class into separate files? This would enable extraction of the respective "generator" methods. However, this approach raises questions regarding linking them based on the target, given that I utilize /// <reference />
linking in the main script file for consolidation.
Any innovative suggestions?
class Option {
///@ts-target ES5
__walkerVariable1:any undefined
__walkerVariable2:any undefined
__walkerVariable3:any undefined
walker() {
/* Pseudo-Walker Code for ES5 */
}
///@ts-target ES6
*walker() {
/* Real Walker Code for ES6 */
}
}
Currently, a Pseudo-Generator code is injected into the ES5 version of my script. I aspire to address this challenge by implementing a different method/function that houses my personal Pseudo-Generator. Therefore, I seek guidance on how to instruct TypeScript to disregard the Pseudo-Generator in ES6/Real-Generator in ES5 and only include one of them based on the chosen target
option.