定制扩展方法在打字稿中

发布于 2025-02-10 07:45:59 字数 1110 浏览 1 评论 0 原文

好吧,我试图为我的界面学生创建扩展方法

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

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

,当我将光标放在学生身上时: - `

`学生''仅引用一种类型,但在这里被用作值。 正在引用本文: - https://www.c-sharpcorner.com/article/article/article/learn-about-elearn-about-extension-extension-extension-method-mothod-mothod-mothod-mothod-mothod-method-method-in-typescript/#:~ :text =扩展%2DMethod%20GIVES%20 you%20,任何%20DATA%2dtype%20You%20want

我可以注意到的是; - 当我们扩展界面类(例如字符串,数字,数组)的类时。扩展方法是可能的。那为什么不作为上述示例。 请帮我!解决此错误

非常感谢:)

编辑: - 因此,我们发现此TS扩展方法回购: - https://github.com/github.com/staeke/staeke/ts-ts--ts---------扩展方法

Well I was trying to create an extension method for my interface 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.
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.

What I could notice is;- when we extend class for Interface like String, Number, Array..Extension method is possible. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

初见终念 2025-02-17 07:45:59

接口是编译的时间元素。当您将打字稿编译到JavaScript中时,它不会为接口发出任何内容。 接口纯粹存在于编译时(受益于打字稿)。

它适用于字符串,数字等的原因,因为它们存在于运行时。

如果您需要对函数的不同实现

  • ,那么为什么不声明 student class 具有默认函数实现的,并允许子类在需要时覆盖功能实现。

如果您想要静态实现

  • ,请简单地将其声明为 Student 类中的静态功能

interface are a compiled time element. When you compile Typescript into Javascript it does not emit anything for an interface. The interface purely exist at compile time (to benefit typescript).

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.

If you want a static implementation

  • then simply declare it as a static function inside the Student class
冷情妓 2025-02-17 07:45:59

Student 接口,而不是您应该在进行任何更改之前创建类并导入该类别。也许最好只是创建新课程。如果您从服务器中获取学生数据,也可以向其添加序列化。

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);

Student is an interface, not class you should create class and import it before making any changes. 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文