简体   繁体   English

Typescript实现接口属性

[英]Typescript implementing interface property

I have declared interface as below 我已经声明接口如下

interface Base {
    required: string;
}

I have implemented interface in class like 我已经在类中实现了接口

class MyClass implements Base{
    method(): void {
        console.log(this.required);
    }
}

But I am getting following error 但我收到以下错误

severity: 'Error' message: 'Class 'MyClass' incorrectly implements interface 'Base'. 严重性:'错误'消息:'类'MyClass'错误地实现了接口'Base'。 Property 'required' is missing in type 'MyClass'.' 类型“ MyClass”中缺少属性“ required”。 at: '5,7' source: 'ts' 来源:“ 5,7”来源:“ ts”

severity: 'Error' message: 'Property 'required' does not exist on type 'MyClass'.' 严重性:“错误”消息:类型“ MyClass”上不存在“必需的属性”。 at: '7,26' source: 'ts' 来源:“ 7,26”;来源:“ ts”

if I declare required: string; 如果我声明required: string; once again in class then no error 再次上课,然后没有错误

interface Base {
    required: string;
}

class MyClass implements Base{
 required: string;

    method(): void {
      this.required="ddd";
        console.log(this.required);
        // you can access HTMLElement
    }
}

var ss=new MyClass();
ss.method();

That's how interfaces work. 接口就是这样工作的。 If you define a property in the interface then you need to define the same property in the class where you are implementing the interface. 如果在接口中定义属性,则需要在实现接口的类中定义相同的属性。 If you would like to use required property without re define the property, you should create a class an extend it. 如果您想使用必需的属性而不重新定义该属性,则应创建一个扩展它的类。

Your error is correct. 您的错误是正确的。 If your class implements an interface , it must also implement all the required properties and methods . 如果您的class实现了interface ,则它还必须实现所有必需的属性和方法 If you want not to implement some properties or methods, you can declare them as optional with ? 如果不想实现某些属性或方法,可以使用?它们声明为optional属性? symbol. 符号。

interface Base {
    required: string;
    someProperty?: string; // << look at the symbol `?` in the end of the property
}

Here you can implement the interface and left the someProperty 在这里,您可以实现接口并保留someProperty

class MyClass implements Base{
    required: string;
    // someProperty is missing here, because it is optional     

    method(): void {
      this.required="ddd";
        console.log(this.required);
        // you can access HTMLElement
    }
}

And not only you can implement interfaces. 而且不仅可以实现接口。 Also you can use them as a type. 您也可以将它们用作类型。 If you have an interface 如果有界面

interface Base {
   required: string;
}

you can create an object which is the type of that interface 您可以创建一个对象,该对象是该接口的类型

const obj: Base = { };

But here you will get an error because if your object is of type Base , you need to provide all required properties. 但是这里会出现错误,因为如果您的对象属于Base类型,则需要提供所有必需的属性。 So you need to write 所以你需要写

const obj: Base = { required: 'Yes' };

This will protect your code from logical errors and your code will be strong typed also for object, for which you don't want to create a class, but you want to said what shape it must be. 这样可以保护您的代码免受逻辑错误的侵害,并且您也可以为对象创建强类型的代码,而您不想为其创建类,但是您想要说出它必须是什么shape

Example

You have an interface 你有一个界面

interface Name {
   name: string
}

and have classes 并上课

class Car implements Name {
    name: string;
    engine: string
    constructor(name: string, engine: string){
       this.name = name;
       this.engine = engine;
    }
}

class Person implements Name {
    name: string;
    surname: string;

    constructor(name: string, surname: string){
       this.name = name;
       this.surname = surname;
    }
}

var arr: Name = [new Car('Car', 'JZ'), new Person('Name', 'Surname')];

here arr is an array of type Name . 这里arrName类型的数组。 So if you get arr[0] , and call on it .engine , intelisense will throw an error that there is no engine property in type Name . 因此,如果您获得arr[0]并对其调用.engine ,则intelisense将引发错误,表明Name类型没有engine属性。 But you can be sure, that every object in that array has name property, because the type of that array is Name and it has an required property name . 但是您可以确定,该数组中的每个对象都具有name属性,因为该数组的类型为Name ,并且具有必需的属性name

If you don't want to delcare requried: string twice use class instate interface for Base and extends instate implements. 如果您不希望delcare要求requried: string两次对Base使用class instate interface并扩展instate实现。

class Base {
    required: string;
}

class MyClass extends Base{
    method(): void {
      this.required="ddd";
        console.log(this.required);
        // you can access HTMLElement
    }
}

Check it out in the playground . 操场上检查一下。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM