English 中文(简体)
字典认为,“这是抽象的”,因为基类是抽象的,需要新生。
原标题:Typescript believes `this` is abstract because the base class is abstract, requiring nasty cast

我有一个基类,即<代码>abstract, 延伸至HTMLElement, 称CustomElementRegistry.define。 打字像使用<条码>,即,作为第二个论点,因为基类为<条码>。 但是,这似乎不正确,因为最后一类显然永远不会抽象。 我不得不使用下面的ug。 是否有更好的办法这样做?

abstract class CustomElement extends HTMLElement {
  constructor() {
    super();
  }
  // Without a cast, the `this` argument gets this error:
  // tsbug.ts:12:6 - error TS2345: Argument of type  this  is not assignable to parameter of type  CustomElementConstructor .
  //   Type  CustomElement  is not assignable to type  CustomElementConstructor .
  //     Type  CustomElement  provides no match for the signature  new (...params: any[]): HTMLElement .
  register() {
    customElements.define(
     "custom-element",
     this, // This argument gets the error.
     // If you replace the above `this` argument with the one below, it works.
     // this as unknown as new () => HTMLElement,
     {});
  }
}

class MyElement extends CustomElement {
  constructor() {
    super();
    this.register();
  }
}

const e = new MyElement();
问题回答

What you are trying to do is incorrect. An instance of CustomElement should not be trying to register itself with custom elements. That logic is on a higher-level than what belongs inside the class.

考虑如果你写的话会发生什么情况

const a = new MyElement();
const b = new MyElement();

两者都试图登记,并在海关要素登记处重新登记。 此外,每当你试图在你的标记中使用习俗要素时,都会发生这种情况。 这并不好。

相反,你的法典应当模仿更接近于:

customElements.define("custom-element", MyElement, {});
const a = new MyElement();
const b = new MyElement();

Design issues aside, what you are looking for is not possible to do within a proper OO paradigm.

某类人没有静态地提到自己,可以动态地加以利用。 文本正确地指出了一个问题:

customElements.define(
     "custom-element",
     this, 
// This is a reference to an *instance* of a class, not to a constructor. 
/// Casting will not make it work. You need a reference to the class you want to register.
     {});

The second argument of customElements.define must be an instance of CustomElementConstructor, which is defined in "lib.dom.d.ts" as follows:

interface CustomElementConstructor {
    new(...params: any[]): HTMLElement;
}

So, your code errors because, instead of passing a constructor as second argument, you are passing an instance of (a subclass of) HTMLElement.

尽管如此,这只是而不是界定和登记习俗超文本元素的正确方式。 如果你(在评论中)问,作为开端人,这里的链接是相关文件的链接:





相关问题
Using a VB.NET DLL file in C++ - class is abstract

I created a VB.NET DLL file which I am using in an unmanaged C++ project. When I try to create an object of the class, I am getting an error: cannot instantiate abstract class Why would my class ...

Abstract class design

Is this an acceptable design ?? Abstract class public abstract class SomethingBase { public abstract int Method1(int number1); public abstract int Method2(int number2); } public class ...

abstract-class in java

Is possible to create objects of an abstract class in Java

abstract classes and interfaces best practices in java

So you ve got an interface and an abstract class that implements a subset of the methods in the interface. You ve also got some classes that inherit the abstract class and give implementations of the ...

Iphone SDK selector or abstract class

I am developing a special type of view controller for an iPhone component library. I have got the who view controller working well, but I need to change it so that it works in one of two ways: ...

What is an alternative to having static abstract methods?

I m having some problems trying to figure out how to solve a problem without being able to have static method in an abstract class or interface. Consider the following code. I have many Wizards that ...

Abstract keyword in PHP

Hey, I m quite experienced with PHP but I have no idea what the keyword abstract does when it comes down to object orientated programming. Can anyone explain in plain english what it can be used for? ...

热门标签