[英]how to define generic interface based on class generic
I'm trying to create a new augmented, generic interface based on another interface. 我试图基于另一个接口创建一个新的增强的通用接口。 The base interface defines the properties the object is going to have ( root
). 基本接口定义对象将具有的属性( root
)。 The augmented type should have the same properties, but instead of any
as a value, I get a more complex object. 增强类型应该具有相同的属性,但是我得到了一个更复杂的对象,而不是any
值。 Note, that the interface IStyleObj
is used inside a generic class, where the generic type is passed to the generic interface. 请注意,接口IStyleObj
在通用类内部使用,其中通用类型被传递到通用接口。
In my experiments I get the these errors, and I don't know how to fix them: 在我的实验中,我得到了这些错误,但我不知道如何解决:
"IStyleObj<T>"
'T' is declared but its value is never read.ts(6133)
"Key in keyof"
A computed property name must be of type 'string', 'number', 'symbol', or 'any'.ts(2464)
Member '[K in keyof' implicitly has an 'any' type.ts(7008)
"... T"
Cannot find name 'T'.ts(2304)
"class: any"
'any' only refers to a type, but is being used as a value here.ts(2693)
This is my current code: 这是我当前的代码:
// Child.ts
interface IStyles {
root?: any
}
// Base.ts
interface IStyleObj<T> {
[K in keyof T]: {
class: any
style: any
}
}
export default class Base<IStyles = {}> {
get styles (): IStyleObj<IStyles> {
// ...
}
}
Interfaces can't be mapped types, only type aliases can. 接口不能被映射为类型,只有类型别名可以。 This will work: 这将起作用:
// Child.ts
interface IStyles {
root?: any
}
// Base.ts
type IStyleObj<T> = {
[K in keyof T]: {
class: any
style: any
}
}
export default class Base<IStyles = {}> {
get styles (): IStyleObj<IStyles> {
return null!
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.