2017-09-27 9 views
1

IUserDifferentGroup[]を返しますがIUserGroupIUserDifferentGroupの両方が同じフィールドはさらにIUserGroupは新しいタイプへの応答をマッピングする方法を、いくつかのより多くを持っていますか?Angular2 typescriptですマップ応答

、この内部は購読
interface IUserDifferentGroup{ 
    Name: string; 
    Code: string; 
    Id: number; 
} 

interface IUserGroup { 
    Id: number; 
    GroupName: string; 
    Visible: boolean; 
    IsDefault: boolean; 
    CanAssociated: boolean; 
} 

答えて

3

使用、コメントを1として

this.service.getUserGroups().subscribe(g => this.userGroups = g as IUserGroup[]); 

EDIT

、修正、

interface IUserDifferentGroup { 
    Name: string; 
    Code?: string; // <-- made this optional 
    Id: number; 
} 

interface IUserGroup { 
    Id: number; 
    GroupName: string; 
    Visible: boolean; 
    IsDefault: boolean; 
    CanAssociated: boolean; 
} 

を購読変更
this.service.getUserGroups().map((g) => return {Name: g.GroupName, Id: g.Id}) 
.subscribe(g => { 
    this.userGroups = g as IUserGroup[]; 
}); 

修正してください。

+0

私は他のタイプと互換性がないことを知りました。正直なところIUserGroupはIUserDifferentGroupと同じですが、さらに2つのフィールド – kosnkov

+0

があります。違いを見るために 'type'sを両方とも表示してください – amal

+0

' interface'定義のそれらのプロパティに '?'を使って 'IUserGroup'にある2つのフィールドをオプションにすることができます。 – amal

関連する問題