7

Variables File:

export class VariableSettings {

   public static string_value: string = 'vikas/${id}/data';


}

Other File.

import {VariableSettings} from './variables';


    getData(id:string ){
      console.log(`${VariableSettings.string_value}`, `${id}`); 

      // it prints vikas/${id}/data abcd11123

    }`

Now I want the result like that "vikas/abcd11123/data". So how can I inject the id in that string.

Any suggestion regarding the same will be appreciated.

Thanks

1
  • I think it is complicated, but instead of interpolling string why not just pass a function like: string_value (id) {return `vikas/${id}/data`}. In your case, it needs another treatment like using regex to match ${id} Commented May 7, 2018 at 6:42

3 Answers 3

10

To use interpolated strings you need to use the `` string separator as you do in your second snippet. If you already used a non interpolated string to hold the value of your setting, there is no way you can then interpolate it using the interpolation feature (you could use a regex to perform replacement but that is a bit messy).

The simplest solution is to make the field a function and have id as a parameter

export class VariableSettings {
    public static USER_BOOKINGS = (id: number) => `vikas/${id}/data`;
}
console.log(`${VariableSettings.USER_BOOKINGS(10)}`);
console.log(VariableSettings.USER_BOOKINGS(10)); // Or no interpolation at call site, not needed anymore if you just need the single value

The USER_BOOKINGS will now be a function that takes as arguments the parameters needed to construct the string. This way the parameters needed for the strings are clear and type-safe.

Sign up to request clarification or add additional context in comments.

3 Comments

public static string_value: string = (id: string) => vikas/${id}/data; It throws error Type '(id: string) => string' is not assignable to type 'string'.
@VIKASKOHLI it's no longer a string, it's a function that returns a string. Remove the :string it is not necessary, type will be inferred based on assigned arrow function
Ok. I will mark this as an answer. Thanks for your valuable time
2

you can try:

public static string_value: string = 'vikas/id/data';

let re = /id/gi; 
let newstr = `${VariableSettings.USER_BOOKINGS}`.replace(re, `${id}`);
console.log(newstr); 

1 Comment

I don't want to use any regex pattern for that. I just want to do interpolation
1
     export class VariableSettings {
       id:number;
       public static string_value: string;


    constructor(){
      this.id = 123456
      VariableSettings.string_value = `vikas/${this.id}/data`;
}
  ngOnInit() {
    console.log(VariableSettings.string_value);
  }
    }

6 Comments

Error:- Cannot find name 'id'.
Please see my newly updated code below which we commited
It will not change the id. Also, we are using two variables. That not what I want to use
If you are not changing the id then put it directly in the string_value @VIKASKOHLI
If I am not changing then I will not ask this question. Because then it is a static value and I can directly store like "/vikas/12345/data"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.