2017-01-13 1 views
0

인터페이스라는 유형의 AddressInterface가있는 address라는 속성을 가진 사람이라는 인터페이스가 있습니다. 다른 인터페이스의 속성을 갖는 것이 맞습니까? 아니면 주소 인터페이스를 구현하는 클래스 주소 여야합니까?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

타이프 스크립트는 구조적으로 형식화 된 언어이므로 위와 같은 작업을 수행하는 것은 전적으로 허용됩니다. 메서드가없는 한 클래스에서 구현할 필요가 없습니다. – 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를 구현하는 클래스의 struture이다 {

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"; 
} 

인터페이스 정의 : - 는 "인터페이스는 이러한 유형의 이름의 역할을 작성하고 귀하의 코드 내에서 계약을 정의하는 강력한 방법입니다 프로젝트 외부의 코드와의 계약 "이라고 말합니다.

관련 문제