Creating a simple flat class like this functions smoothly:
class Test {
company_name: string = "";
company_id: number = 0;
company_website: string = "";
}
When I instantiate it with let product = new Test()
, everything works as anticipated, and product
is initialized with the default values.
Is there a way to achieve the same outcome for non-flat class variables? Ideally, it should function similar to this example (which currently fails):
class Test {
companyData: {
company_name: string = "";
company_id: number = 0;
company_website: string = "";
}
productData: {
category_id: number = 0;
product_name: string = "";
price: {
price_in: number = 0;
price_out: number = 0;
}
}
}
The error message in VSCode reads
A type literal property cannot have an initializer
. For my specific scenario, it's crucial that all variables are initialized and can be assigned default values.