0

I want to parse the nameOfTheComponent constant and want to create a variable name like I{nameOfTheComponent}Props

An example here:

const nameOfTheComponent = 'Test';

export interface ITestProps { // Props name should be ' I{nameOfTheComponent}Props '
    test?: any; // any, string, number, boolean,
}

Is it possible?

osmancalisir
  • 147
  • 2
  • 10
  • 2
    For runtime, see [“Variable” variables in Javascript?](https://stackoverflow.com/q/5187530). It's a ***TERRIBLE*** idea to create variables on the fly. But you seem to want to do this at compile time which makes less sense. What is the point of programmatically generating your source code only to compile and discard it later? It doesn't save you that much effort. And you'd have trouble convincing your tools, intellisense, etc. to work with the auto-generated code. – VLAZ May 18 '21 at 11:20
  • 1
    What are you ***actually*** trying to accomplish? – Wolf May 18 '21 at 11:24
  • 1
    @Wolf my guess is OP wants to save some typing by defining a string and using it to generate (part of?) the rest of the code in the file. So, there is just copy/pasted `IProps` (or similar) everywhere and and then just the `` part is supplied. – VLAZ May 18 '21 at 11:31
  • you are right @VLAZ and I actually do not 'have to' because it is using only for few places but this will be later created many times. It is totally okay to spend my time giving them names. But wonder if this possible technically in TypeScript :) – osmancalisir May 18 '21 at 11:51
  • 1
    Sure, write your own compiler plugin for TS. – VLAZ May 18 '21 at 11:52

0 Answers0