[英]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/core
的Type<T>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.