在其他类成员中使用类成员的字段
我正在制作ECS,这是我对系统类的代码,它将保持行为:
type Trigger<Components extends Component, Query = Components['type'][]> = (...components: ComponentSet<Components, Query>) => void;
interface Query<Components extends Component> {
withAll: Components['type'][]
}
abstract class System<Components extends Component> {
query: Query<Components>;
constructor(query: Query<Components>) {
this.query = query;
}
abstract onFrame: Trigger<Components, this.query.withAll>;
}
ComponentSet是一种采用组件类型的类型([“健康”,“库存”)并将其转换为其实际的组件值({health:number},{activeLot:number})。奇怪的是,以上是不起作用的,我在最后一行中遇到了语法错误,宣布一个抽象成员的flaframe。但是,当我将此更改时
abstract onFrame: Trigger<Components, ["Health"]>;
。为什么字面的工作正常,但是尽管两者都具有相同的类型,但可变访问不起作用?有没有办法解决这个问题或解决这个问题?
仅供参考组件['type']是一个字符串,我在其他地方定义的组件接口确保了这一点。
I'm making an ECS and this is the code I have for the System class, which will hold behavior:
type Trigger<Components extends Component, Query = Components['type'][]> = (...components: ComponentSet<Components, Query>) => void;
interface Query<Components extends Component> {
withAll: Components['type'][]
}
abstract class System<Components extends Component> {
query: Query<Components>;
constructor(query: Query<Components>) {
this.query = query;
}
abstract onFrame: Trigger<Components, this.query.withAll>;
}
ComponentSet is a type that takes an array of component types (["Health", "Inventory"]) and transforms it into its actual component values ({health: number}, {activeSlot: number}). What is strange is that the above does not work, I get a syntax error on the last line, the one declaring an abstract member onFrame. But when I change this.query.withAll to a literal, such as:
abstract onFrame: Trigger<Components, ["Health"]>;
Then TypeScript throws no issue. Why is it that a literal works fine but a variable access does not work, despite both having the same type? Is there a way to work around this or solve this?
FYI Components['type'] is a string, the Component interface I've defined elsewhere ensures this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论