I'm trying to create a single 'config' object for my site. One config value is 'email', and another is an array of 'faqs'. I'd like to refer to email value within the faqs, but get the following error: Block scoped variable 'used' before it's declaration.
I understand the error, but am looking for a way around this so I don't have to hard code our email in multiple places.
Some sample code to demonstrate what I'd like to do (this obviously breaks)
interface Config {
email: string;
faqs: Array<FAQ>;
}
export interface FAQ {
question: string;
answer: string;
}
export const config: Config = {
email: "fake@gmail.com",
faqs: [
{
question: 'simple question',
answer: 'I want to refer to ' + config.email + ' here'
},
],
You can solve this problem by using a getter for the faqs property. A getter lets you dynamically access the email value without encountering the issue of trying to use a variable before it’s been fully defined.
interface Config {
email: string;
faqs: Array<FAQ>;
}
export interface FAQ {
question: string;
answer: string;
}
export const config: Config = {
email: "fake@gmail.com",
get faqs(): Array<FAQ> {
return [
{
question: "simple question",
answer: `I want to refer to ${this.email} here`,
},
];
},
};