Typescript将接口类型声明为另一种接口类型

时间:2017-01-13 02:20:16

标签: class typescript types interface

我有一个名为person的接口,它有一个名为address的属性,它有一个AddressInterface类型,它是一个接口。拥有另一个接口的属性是正确的还是应该是一个实现地址接口的类地址?

PersonInterface

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

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

AddressInterface

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

1 个答案:

答案 0 :(得分:0)

我使用了testclass来实现PersonInterface的所有属性。我认为这是遵循结构的最佳方式。 这是阶级结构

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

接口定义: -  "接口充当了命名这些类型的角色,是一种在代码中定义合同以及与项目之外的代码签订合同的强大方式,并且#34;。

相关问题