[英]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.