【问题标题】:Typescript Declare Interface Type as another Interface TypeTypescript 将接口类型声明为另一种接口类型
【发布时间】:2017-05-28 08:11:34
【问题描述】:

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

人机界面

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

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

地址接口

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

【问题讨论】:

  • 是的,对我来说,前者似乎是更好的选择。也就是说,使用AddressInterface
  • 同意,因为 typescript 是一种结构类型的语言,所以完全可以接受您在上面所做的事情。除非它也有方法,否则无需在类中实现它

标签: class typescript types interface


【解决方案1】:

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

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

接口定义:- “接口扮演命名这些类型的角色,并且是在代码中定义合同以及与项目外部代码签订合同的强大方式”。

【讨论】:

    猜你喜欢
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-19
    • 2021-06-07
    • 2020-09-29
    • 2010-09-23
    • 2016-01-01
    相关资源
    最近更新 更多