简体   繁体   English

您可以在实现另一个接口的组件的接口中键入字段吗?

[英]Can you type a field in an interface to a Component that implements another interface?

I have created an interface for my components to implement so that each component created for this type have the same fields. 我已经为我的组件创建了要实现的接口,以便为此类型创建的每个组件都具有相同的字段。 I then have another interface which has a field that requires this type of component. 然后,我有另一个接口,该接口的字段需要这种类型的组件。

I have created these interfaces and setup the corresponding components and objects yet I still get the following error: 我已经创建了这些接口并设置了相应的组件和对象,但是仍然出现以下错误:

Type 'typeof MyComponent' cannot be assigned to type 'ComponentShouldImplementThis<T>'. Property 'someField' is missing in type 'typeof MyComponent'.

My interfaces: 我的界面:

export interface ComponentShouldImplementThis<T> {
  someField: string;
}

export interface ContainsFieldComponentInterface<T> {
  component: ComponentShouldImplementThis<T>;
}

My Component: 我的组件:

@Component({
 selector: 'mycomponent',
 template: '<div>Hello</div>'
})
export class MyComponent implements ComponentShouldImplementThis<any> {
  someField: string;
  constructor(){}
}

My Object that yells at me: 我对我大吼的对象:

let implementedInterface: ContainsFieldComponentInterface = {
  component: MyComponent
}

someField satisfies the IDE when implementing ComponentShouldImplementThis but the interface for having a component that implements it, does not like. 当实现ComponentShouldImplementThis时, someField满足IDE,但是不喜欢具有用于实现它的组件的接口。

Something I got to get angular compiled was doing an instance of the component instead: 我要进行角度编译的事情是代替组件的一个实例:

let implementedInterface: ContainsFieldComponentInterface = {
  component: new MyComponent
}

But this causes some other issues downstream. 但这会在下游引起其他问题。 Is this the correct implementation? 这是正确的实现吗? Not sure where I am going wrong. 不知道我要去哪里错了。

There is a type in angular/core that represents Component: https://angular.io/api/core/Type angular/core中有一个表示组件的类型: https : //angular.io/api/core/Type

Usage: 用法:

export interface ContainsFieldComponentInterface<T> {
  component: Type<ComponentShouldImplementThis<T>>;
}

Why is your type information not working? 为什么您的类型信息不起作用? You probably remember that classes in JS/TS are Functions (or constructor functions) that create objects. 您可能还记得JS / TS中的类是创建对象的函数(或构造函数)。 You specify the type of the created object in your interface (verifying that the created object will have all the properties of a class). 您可以在界面中指定创建对象的类型(验证创建的对象将具有类的所有属性)。 If want to narrow down the possible constructor functions you have to create another type: 如果想缩小可能的构造函数,则必须创建另一种类型:

type OfSpecificType<T> = {
   new (...args: any[]): T
}

This signature means that calling a function of OfSpecificType type will return the type T . 此签名意味着调用OfSpecificType类型的函数将返回类型T

So your interface will end up looking like: 因此,您的界面最终看起来像:

export interface ContainsFieldComponentInterface<T> {
  component: OfSpecificType<ComponentShouldImplementThis<T>>;
}

but it's the same as using angular/core 's Type<T> 但这与使用angular/coreType<T>

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

相关问题 如何呈现实现接口的组件? - How can I render a component that implements an interface? 我不明白为什么 Angular 组件 class 添加实现 onInit 接口 - I can't figure out why Angular component class add implements onInit interface 无法扩展接口“http.IncomingMessage”。 您指的是 &#39;implements&#39; 吗? - Cannot extend an interface 'http.IncomingMessage'. Did you mean 'implements'? 类错误地实现了接口“ OnChanges”。 类型中缺少属性“ ngOnChanges” - Class incorrectly implements interface 'OnChanges'. Property 'ngOnChanges' is missing in type 如何在Typescript中将组件类定义为接口字段? - How to define a component class as an interface field in Typescript? 如何在另一个组件中使用接口? (得到错误) - How to use the Interface in another component? (getting error) Typescript将接口描述的类型转换为另一种类型? - Typescript convert a type described by an Interface to another? 我可以在 html 组件中使用接口,但不能在 typescript 中使用 - I can use an interface in the html component but not in the typescript Angular 生命周期错误 - 错误地实现了接口“DoCheck” - Angular lifecycle error - incorrectly implements interface 'DoCheck' 错误类“FirebaseApp”错误地实现了接口“App” - ERROR Class 'FirebaseApp' incorrectly implements interface 'App'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM