从 index.ts 以外的文件中导出类

Posted

技术标签:

【中文标题】从 index.ts 以外的文件中导出类【英文标题】:Export class from files other than index.ts 【发布时间】:2019-07-04 07:35:19 【问题描述】:

我创建了一个名为assembly/Vec3.ts 的文件,内容如下:

/**
 * @constructor
 * @name pc.Vec3
 * @classdesc A 3-dimensional vector.
 * @description Creates a new Vec3 object.
 * @param Number [x] The x value. If x is an array of length 3, the array will be used to populate all components.
 * @param Number [y] The y value.
 * @param Number [z] The z value.
 * @example
 * var v = new pc.Vec3(1, 2, 3);
 */
export class Vec3 
    x: number;
    y: number;
    z: number;

    // AS is more strict than TS... need to replace all occuranves of this in PlayCanvasTS at some point
    //constructor(x?: any, y?: number, z?: number)
    constructor(x: number, y: number, z: number)
    
        //if (x && x.length === 3) 
        //  this.x = x[0];
        //  this.y = x[1];
        //  this.z = x[2];
        // else 
        //  this.x = x || 0;
        //  this.y = y || 0;
        //  this.z = z || 0;
        //
        this.x = x;
        this.y = y;
        this.z = z;
    

    /**
     * @function
     * @name pc.Vec3#add
     * @description Adds a 3-dimensional vector to another in place.
     * @param pc.Vec3 rhs The vector to add to the specified vector.
     * @returns pc.Vec3 Self for chaining.
     * @example
     * var a = new pc.Vec3(10, 10, 10);
     * var b = new pc.Vec3(20, 20, 20);
     *
     * a.add(b);
     *
     * // Should output [30, 30, 30]
     * console.log("The result of the addition is: " + a.toString());
     */
    add(rhs: Vec3): Vec3 
        this.x += rhs.x;
        this.y += rhs.y;
        this.z += rhs.z;

        return this;
    

    /**
     * @function
     * @name pc.Vec3#add2
     * @description Adds two 3-dimensional vectors together and returns the result.
     * @param pc.Vec3 lhs The first vector operand for the addition.
     * @param pc.Vec3 rhs The second vector operand for the addition.
     * @returns pc.Vec3 Self for chaining.
     * @example
     * var a = new pc.Vec3(10, 10, 10);
     * var b = new pc.Vec3(20, 20, 20);
     * var r = new pc.Vec3();
     *
     * r.add2(a, b);
     * // Should output [30, 30, 30]
     *
     * console.log("The result of the addition is: " + r.toString());
     */
    add2(lhs: Vec3, rhs: Vec3): Vec3 
        this.x = lhs.x + rhs.x;
        this.y = lhs.y + rhs.y;
        this.z = lhs.z + rhs.z;

        return this;
    

然后通过npm run asbuild构建它

但该文件只是被忽略,不包含在 untouched.wasm 中。

是否可以从所有文件中导出类?

【问题讨论】:

【参考方案1】:

目前我通过在doit.sh 中将Vec3.ts 添加到asc 来解决它:

# print the commands via -x/+y
set -x
npx asc assembly/index.ts assembly/Vec3.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --debug
npx asc assembly/index.ts assembly/Vec3.ts -b build/optimized.wasm -t build/optimized.wat --sourceMap --validate --optimize
set +x

【讨论】:

【参考方案2】:

目前,AssemblyScript 仅支持从入口文件 (index.ts) 全局导出。因此,您应该将所有实体重新导出到此文件。但这可能会在未来有所改善。请参阅此讨论:https://github.com/AssemblyScript/assemblyscript/issues/464

【讨论】:

以上是关于从 index.ts 以外的文件中导出类的主要内容,如果未能解决你的问题,请参考以下文章

Typescript:如何从 javascript 文件中导入类?

为啥不能在 TypeScript 中导出类实例?

导出 `index.ts` 中的所有文件

是否可以在也具有 IPC 挂钩的 node.js 文件中导出类?

在 typescript 包中导出多个函数

如何在打字稿monorepo中导入本地包