简体   繁体   English

我有一个包含 200 多个属性的界面。 class 如何在不再次声明所有 200+ 的情况下实现此接口?

[英]I have an interface with 200+ properties. How can a class implement this interface without declaring all 200+ again?

I have an interface with 200+ properties and a class that uses these same properties.我有一个具有 200 多个属性的接口和一个使用这些相同属性的 class。

interface MyProperties {
  property1: string
  property2: string
  ...
  property232: string
}

class MyClass {
  constructor() {
    this.property1 = 'foo';
    this.property2 = 'bar';
    ...
    this.property232 = 'bar';
  }
}

Right now typescript will give me this error: Property 'property1' does not exist on type 'MyClass'.现在 typescript 会给我这个错误: Property 'property1' does not exist on type 'MyClass'.

Is there a way to tell typescript that my class properties come from the MyProperties interface?有没有办法告诉 typescript 我的 class 属性来自MyProperties接口?

I dont want to do something like this:不想做这样的事情:

class MyClass {
  property1: string
  property2: string
  
  constructor() {
    this.property1 = 'foo';
    this.property2 = 'bar';
  }
}

since the interface is already used extensively in my code and I don't want to repeat these typings.因为该接口已经在我的代码中广泛使用,我不想重复这些类型。

Closest I've come is this我来的最近的是这个

class MyClass {
  property1: MyProperties['property1'];
  property2: MyProperties['property2'];
  ...
  property232: MyProperties['property232'];

  constructor() {
    this.property1 = 'foo';
    this.property2 = 'bar';
    ...
    this.property232 = 'bar';
  }
}

but it's not super pretty, since in my actual code I have 200+ properties但这不是超级漂亮,因为在我的实际代码中我有 200 多个属性

Well just add an implements MyProperties clause to the class:那么只需在 class 中添加一个implements MyProperties子句:

interface MyProperties {
    property1: string
    property2: string
}

class MyClass implements MyProperties {
    property1: string;
    property2: string;

    constructor() {
        this.property1 = 'foo';
        this.property2 = 'bar';
    }
}

Right now typescript will give me this error现在 typescript 会给我这个错误

Thats because there are no definition in class for this properties.那是因为class中没有此属性的定义。 You should tell TypeScript compiler that MyClass has this properties:您应该告诉 TypeScript 编译器MyClass具有以下属性:

class MyClass {
    property1: string;
    property2: string;
    constructor() {
        this.property1 = 'foo';
        this.property2 = 'bar';
    }
}

Now, compiler know what properties MyClass has and can realize that MyClass implementing MyProperties interface:现在,编译器知道MyClass有哪些属性,并且可以实现MyClass实现MyProperties接口:

interface MyProperties {
    property1: string
    property2: string
}

class MyClass {
    property1: string;
    property2: string;
    constructor() {
        this.property1 = 'foo';
        this.property2 = 'bar';
    }
}

const a: MyProperties = new MyClass(); // no error here

Also you can explicitly implement MyProperties interface to improve readability of code using implements MyProperties :您还可以使用implements MyProperties显式实现MyProperties接口以提高代码的可读性:

interface MyProperties {
    property1: string
    property2: string
}

class MyClass implements MyProperties {
    property1: string;
    property2: string;
    constructor() {
        this.property1 = 'foo';
        this.property2 = 'bar';
    }
}

const a: MyProperties = new MyClass(); // no error here too

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以让 class 实现一个接口而不重新输入所有类型吗? - Can I have a class implement an interface without re-typing out all the types? NextJS api 路由在本地完美运行(创建 200 多条记录)并且在 Vercel 上损坏 - NextJS api route works perfectly locally (creates 200+ records) and it's broken on Vercel 以一种方式实现接口,当一个类实现它时,它**必须**将接口的所有属性实现为可选或非可选 - Implement interface in a way that when a class implements it, it **must** implement all properties of the interface either as optional or not optional 为什么抽象 class 必须实现接口中的所有方法? - Why does abstract class have to implement all methods from interface? 我想列出类/接口的所有公共属性 - I want to list all public properties of a Class/Interface 我可以使用服务(Typescript类)和接口来实现常量和枚举吗? - Can I implement constants and enums with a service (Typescript Class) and an Interface? 扩展接口属性而无需声明新接口 - Extend property of interface without declaring a new interface 如何编写一个可以在 TypeScript 中实现接口的类 - How to write a class which can implement an interface in TypeScript 如何使类符合接口 - How can I make a class conform to an interface 声明一个接口,其中所有属性均符合类型,而无需扩展接口 - Declare an interface where all properties conform to a type, WITHOUT widening the interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM