You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在AssemblyScript中将类作为命名空间的一部分重新导出?

在AssemblyScript中将类重新导出到命名空间的正确方法

你之前的写法无法编译,是因为export class Action = EntityAction;不是合法的AssemblyScript语法——不能用这种赋值方式把已有的类绑定到命名空间的成员上。

在AssemblyScript中可以实现你的需求,正确的做法是使用export import语法在命名空间内重新导出这些类:

import {
  Action as EntityAction,
  Asset as EntityAsset,
  Batch as EntityBatch,
  Batcher as EntityBatcher,
  Stream as EntityStream,
  Watcher as EntityWatcher,
} from "./bindings/schema";

export namespace Entity {
  export import Action = EntityAction;
  export import Asset = EntityAsset;
  export import Batch = EntityBatch;
  export import Batcher = EntityBatcher;
  export import Stream = EntityStream;
  export import Watcher = EntityWatcher;
}

这种写法会把每个导入的类直接挂载到Entity命名空间下,外部代码可以通过Entity.Action、Entity.Asset等方式访问这些类,同时完全保留类的原有功能(比如构造函数、实例方法等)。

你也可以简化导入流程,直接在命名空间内完成导入和导出:

export namespace Entity {
  export import Action = require("./bindings/schema").Action;
  export import Asset = require("./bindings/schema").Asset;
  export import Batch = require("./bindings/schema").Batch;
  export import Batcher = require("./bindings/schema").Batcher;
  export import Stream = require("./bindings/schema").Stream;
  export import Watcher = require("./bindings/schema").Watcher;
}

两种写法效果一致,可根据代码风格偏好选择。

内容的提问来源于stack exchange,提问作者Paul Razvan Berg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 02:25:59