简体   繁体   English

打字稿中的自定义扩展方法

[英]Custom extension method in typescript

Well I was trying to create an extension method for my interface Student好吧,我正在尝试为我的界面 Student 创建一个扩展方法

declare global {  
  interface Student {  
   CourseOpted(): String;  
  }  
 }  

 Student.prototype.CourseOpted = function(): string {  
  return 'some-string';
 }  
 export {}; 

And when I place cursor on Student:- Getting this error当我将光标放在学生上时:-出现此错误

'Student' only refers to a type, but is being used as a value here. 'Student' 仅指一种类型,但在这里用作值。 Was referring this article:- https://www.c-sharpcorner.com/article/learn-about-extension-methods-in-typescript/#:~:text=Extension%2Dmethod%20gives%20you%20the,any%20data%2Dtype%20you%20want .指的是这篇文章:- https://www.c-sharpcorner.com/article/learn-about-extension-methods-in-typescript/#:~:text=Extension%2Dmethod%20gives%20you%20the,any% 20data%2Dtype%20you%20want

What I could notice is;- when we extend class for Interface like String, Number, Array..Extension method is possible.我能注意到的是;- 当我们为接口扩展类时,如 String、Number、Array..Extension 方法是可能的。 Then why not for the above example.那为什么不看上面的例子。 Please help me!请帮我! Solve this error解决这个错误

Thanks a lot in advance :)提前非常感谢:)

Edited:- So we found this ts extension method repo:- https://github.com/staeke/ts-extension-methods编辑:- 所以我们找到了这个 ts 扩展方法 repo:- https://github.com/staeke/ts-extension-methods

Student is an interface , not class you should create class and import it before making any changes. Student是一个interface ,而不是class ,您应该在进行任何更改之前创建类并导入它。 Maybe it is better just to create new class.也许创建新课程会更好。 You may also add serialization to it, if you take students data from server.如果您从服务器获取学生数据,您还可以为其添加序列化。

Typescript Playground Link 打字稿游乐场链接

interface IStudent {
  id: number;
  firstName: string,
  lastName: string,
}

class Student implements IStudent {
  id: number;
  firstName: string;
  lastName: string;

  constructor(student: IStudent)
  constructor(id: number, firstName: string, lastName: string)
  constructor(idOrStudent: number | IStudent, firstName?: string, lastName?: string) {
    if (typeof idOrStudent === "object") {
      this.id = idOrStudent.id;
      this.firstName = idOrStudent.firstName;
      this.lastName = idOrStudent.lastName;
      return;
    }
    this.id = idOrStudent;
    this.firstName = firstName as string;
    this.lastName = lastName as string;
  }

  public foo() {
    return "bar";
  }
}

const student0 = new Student(0, "John", "Doe");
console.log(student0);
console.log(student0.foo());

const student1 = new Student({
  id: 1,
  firstName: "Milka",
  lastName: "Helgi"
});

console.log(student1);

interface are a compiled time element. interface是编译的时间元素。 When you compile Typescript into Javascript it does not emit anything for an interface .当您将 Typescript 编译为 Javascript 时,它不会为interface发出任何内容。 The interface purely exist at compile time (to benefit typescript).interface纯粹存在于编译时(有利于打字稿)。

The reason why it works for String, Number, etc. because they exist at runtime.它适用于字符串、数字等的原因是它们存在于运行时。

If you want different implementation of the function如果你想要不同的功能实现

  • then why not declare Student as a class with default function implementation and allow a sub-class to override the functional implementation as and when required.那么为什么不将Student声明为具有默认函数实现的class ,并允许子类在需要时覆盖函数实现。

If you want a static implementation如果你想要一个静态实现

  • then simply declare it as a static function inside the Student class然后只需在Student类中将其声明为静态函数

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

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