Angular 接口中的模板和联合类型

Angular 接口中的模板和联合类型,angular,typescript,google-cloud-firestore,Angular,Typescript,Google Cloud Firestore,我有这个接口 export interface User{ name: string birthday: number | Timestamp ... } 使用strictTemplates:false时,当我获取带有时间戳生日的服务器数据时,以及当我将带有数字生日的数据放入本地存储时,我使用此接口。没有错误。 在strictTemplates:true中,我有以下错误: Types of property 'birthday' are incompatible. Type 'nu

我有这个接口

export interface User{
  name: string
  birthday: number | Timestamp
  ...
}
使用strictTemplates:false时,当我获取带有时间戳生日的服务器数据时,以及当我将带有数字生日的数据放入本地存储时,我使用此接口。没有错误。 在strictTemplates:true中,我有以下错误:

Types of property 'birthday' are incompatible.
Type 'number | Timestamp' is not assignable to type 'number'.
  Type 'Timestamp' is not assignable to type 'number'.
这里的最佳实践是什么?我制作了三个界面:

 //without birthday
export interface User{
  name: string
  ...
}

export interface UserFromFirestore extends User {
  birthday: Timestamp;
}
export interface UserInLocal extends User {
  birthday: number;
}
我的函数(在伪代码中)是:

getUser(id:string):可观察{
此.getDataFromFirestore(id).pipe(
映射((用户:UserFromFirestore)=>userWithBirthdayInNumber(用户)))
}
我能做得更好,对吗?我的解决方案太冗长了,对吗?

您可以在这里使用泛型。 更多信息:

导出用户界面{
名称:string
生日:T
}
obj1:用户;
obj2:用户;
getUser(id: string):Observable<UserInLocal>{
  this.getDataFromFirestore(id).pipe(
  map((user:UserFromFirestore) => userWithBirthdayInNumber(user)))
}
export interface User<T>{
  name: string
  birthday: T
}

  obj1: User<number>;
  obj2: User<Timestamp>;