I'd like to externalize some strings but still use some form of string substitution.
In some of my node
based projects I've used:
var format = require('string-format');
format(Constants.COUNTRY_WEATHER_ENDPOINT, {
country: country
})
However In Typescript
, I've been trying something like this ..
Error:
Cannot find name 'country'.at line 18 col 66 in repo/src/app/constants.ts
Constants.ts
export class Constants {
public static COUNTRY_WEATHER_ENDPOINT = `/api/country/${country}/weather`;
}
TestService.ts
import { Constants } from './constants';
export class TestService {
constructor(private $http) { }
public getWeather(country) {
let url = Constants.COUNTRY_WEATHER_ENDPOINT;
return this.$http.get(
url,
.then(response => response);
}
}
TestService.$inject = ['$http'];
Use arrow functions:
export const Constants: { readonly [name: string]: (key: string) => string } = {
countryWeatherEndpoint: country => `/api/country/${country}/weather`
}
Then do:
import { Constants } from "./constants";
// ...
const url = Constants.countryWeatherEndpoint(country);
// use your url