I've been troubleshooting this issue for hours and I'm stumped by this specific typescript error.
Although I can't share the actual code, I've simplified the problem to the example below:
abstract class CustomerType {
protected abstract customerInfo?: IUserInfo;
}
enum CustomerGroup {
FirstTimer = "first timer",
Subscriber = "subscriber"
}
interface IUserInfo {
group?: CustomerGroup;
firstClassMember?: boolean;
rewardsMember?: boolean;
}
abstract class CustomerType {
protected abstract customerInfo?: IUserInfo;
}
class Default extends CustomerType {
protected customerInfo: IUserInfo = {};
constructor(){
super();
}
get _customerInfo{
return this.customerInfo ?? {}
}
set _customerInfo(customerInput: IUserInfo){
let input: keyof IUserInfo;
for(input in customerInput){
if(customerInput[input] === undefined) continue;
this.customerInfo = customerInput[input];
}
}
}
The Typescript error encountered is as follows:
Type 'boolean | CustomerGroup | undefined' is not assignable to type 'IUserInfo'. Type 'undefined' is not assignable to type 'IUserInfo'.(2322)
Despite the validation just above, Typescript fails to detect that
customerInput[input]
is definitely not undefined.
Check out the Typescript playground link here: TS Playground Link
Take a look at the codepen with the issue: CodePen Link