2017-01-13 8 views
0

インターフェイスと呼ばれるAddressInterfaceの種類を持つaddressというプロパティを持つpersonというインターフェイスがあります。別のインタフェースのプロパティを持つのは正しいのですか、これはアドレスインタフェースを実装するクラスのアドレスですか?Typescript別のインターフェイスタイプとしてインターフェイスタイプを宣言

PersonInterface私はPersonInterface.Iのすべてのプロパティを実装するためにTestClassを使用

export interface AddressInterface{ 
    name:string; 
    line1:string; 
    line2:string; 
    city:string; 
    postalcode:string; 
    region:string; 
    country:string; 
} 
+0

ええ、前者が私にとってより良い選択肢に見えます。つまり、 'AddressInterface'を使用します。 – cartant

+0

合意したように、typescriptは構造的に型付けされた言語なので、上で行ったことを全く受け入れることができます。メソッドを持たない限りクラス内で実装する必要はありません – alechill

答えて

0

import {AddressInterface} from "./address.interface" 

export interface PersonInterface{ 
    firstname:string; 
    lastname:string; 
    dob:string; 
    address:AddressInterface; 
    username:string; 
    email:string; 
} 

AddressInterfaceは、この構造にに従うための最良の方法だと思います。 これはクラスカルコゲノフルバレン骨格

輸出クラスTestClassをがPersonInterface {

firstname = "firstname"; 
    lastname = "lastname"; 
    dob = "12-25-1999"; 
    address = { 
     name: "name", 
     line1: "line1", 
     line2: "line2", 
     city: "city", 
     postalcode: "postalcode", 
     region: "region", 
     country: "country", 
    }; 
    username = "username"; 
    email = "email"; 
} 

インタフェース定義を実装している: - 「のインターフェイスは、これらのタイプの命名の役割を記入し、としてあなたのコード内での契約を定義する強力な方法ですあなたのプロジェクトの外のコードとの契約 "