I currently have this code, which works:
export type {
Action as EntityAction,
Asset as EntityAsset,
Batch as EntityBatch,
Batcher as EntityBatcher,
Stream as EntityStream,
Watcher as EntityWatcher,
} from "./bindings/schema";
All of those Entity
exports are classes. I would like to redeclare them under an Entity
namespace, like so:
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 class Action = EntityAction;
export class Asset = EntityAsset;
export class Batch = EntityBatch;
export class Batcher = EntityBatcher;
export class Stream = EntityStream;
export class Watcher = EntityWatcher;
}
This code doesn't compile, though.
Is it possible to do this in AssemblyScript, or not?
Your code is almost correct for TypeScript, but would use export const
instead of export class
in the body of the exported namespace. Unfortunately, that doesn't work in AssemblyScript. There was some discussion about this in issue #1790, where it was decided to give a better compiler error, but not support that feature directly.
ie., if you try, you'd get something like this:
ERROR AS234: Expression does not compile to a value at runtime.
:
16 │ export const Action = EntityAction;
│ ~~~~~~~~~~~~
└─ in assembly/foo.ts(16,25)
However, if you want to re-export all of the exported items from the original file, that works fine:
import * as Entity from "./bindings/schema";
export { Entity };
I use that technique often.
As a workaround for the original issue, if you don't want all items you can use some intermediate file to re-export only some of the items.
export { Action, Batch, Watcher } from "./bindings/schema";
Then you can import that file instead.
import * as Entity from './theotherfile'