I'm having trouble with this solution. Here is the link to the source:
Is it possible to declare a property with multiple types using the OR notation like this?
export interface OnboardingSchoolModel {
level?: string | number;
school?: string;
}
I attempted this approach but it doesn't seem to be working. Any suggestions on how to work around this issue?
I prefer to restrict the types for the property rather than using any
.
Encountering a compile-time error:
(property) OnboardingSchoolModel.level?: string | number
Argument of type 'string | number' is not assignable to parameter of type 'string'.
Type 'number' is not assignable to type 'string'.
Example of Usage:
getSchools(): Promise<OnboardingSchoolModel[]> {
return new Promise(resolve => {
this.onboardingService.getSchools().pipe(first()).subscribe((schools: any) => {
const schoolList: OnboardingSchoolModel[] = [];
forEach(schools, (s: any) => {
const schoolDocumentData: OnboardingSchoolModel = s.payload.doc.data();
const id: string = s.payload.doc.id;
const school: OnboardingSchoolModel = {
level: this.getIndexForLevel(schoolDocumentData.level),
};
schoolList.push(school);
});
resolve(schoolList);
}, err => console.log(err));
});
}
getIndexForLevel(levelString: string): number {
let levelIndex: number;
switch (levelString) {
case LevelEnum.High_School:
levelIndex = 0;
break;
case LevelEnum.College:
levelIndex = 1;
break;
case LevelEnum.Graduate:
levelIndex = 2;
break;
default:
}
return levelIndex;
}