I have User interface in typescript project (Ionic 2), and I want that one of his variables to have 2 different options as a type.
Example of what I written:
export interface User {
_id: string,
usUnits: boolean,
height?: UsHeightUnits | NotUsHeightUnits
};
interface UsHeightUnits {
feets?: number,
inches?: number
}
interface NotUsHeightUnits {
centimeters?: number
}
This not cause me any errors, the errors comes in another file:
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { User } from '../../interfaces/user/user';
@Component({
templateUrl: 'build/pages/profile-edit/profile-edit.html',
})
export class ProfileEditPage {
public user: User;
constructor(private navCtrl: NavController) {
// Here i have red error underline, under the "centimeters"
this.user.height.centimeters = 4;
}
}
There error is: [ts] Property 'centimeters' does not exist on type 'UsHeightUnits | NotUsHeightUnits'.
In other cases with | (or) it works fine, like this case that using strings and not object:
export interface User {
_id: string,
gender: 'male' | 'female'
};
Suggestions?