在其他类成员中使用类成员的字段

发布于 2025-01-26 16:40:49 字数 941 浏览 2 评论 0原文

我正在制作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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文