The employee type can be either PartTime | FullTime
and is decided at run time. Now since this is decided at runtime, every line where I use employeeType, I am unable to navigate to the desired method using [CMD + Click] on VS Code. Each time I access methods that are not available in all the other Classes (like Bonus method in FullTime Class), I get the following warning
//Property 'Bonus' does not exist on type 'PartTime | FullTime'. Property 'Bonus' does not exist on type 'PartTime'.
In my actual code there are 30+ variables which can be of different types, and based on the runtime value the values are assigned in the respective index files.
I am able to handle this using (employeeObj.employeeType as FullTime).Bonus
, but this needs to be done for each access and I would have 50+ lines that needs to do this.
Is there a way I can specify the type of this variable during import at the top level something like
import {employee. employeeType as FullTime}
or import {employee. employeeType as PartTime}
class Employee
{
employeeType: PartTime | FullTime;
}
class PartTime
{
get Salary() { return 1000; }
}
class FullTime
{
get Salary() { return 2000; }
get Bonus() { return 500; }
}
class Main
{
let employeeObj = new Employee();
let fulltimeEmp = new FullTime()
employeeObj.employeeType = fulltimeEmp;
console.log(employeeObj.employeeType.Bonus)
}
1