Unable to organize my typescript definitions into multiple files

155 Views Asked by At

I am writing a TypeScript definition for a Lua library, to be used with the TypeScript-to-Lua transpiler. I am able to get the definition to work and my code runs exactly as expected, but so far I've been unable to find a reasonable way to split my definition into multiple files.

My project is a math library and I have several Lua files such as vec3.lua, mat4.lua, quat.lua, etc. Each of these submodules has a new function for creating an "object" in Lua using metamethods. new is also being called by the __call metamethod so that if you call the Lua table like a function, it will spit out an object.

local cpml = require "cpml"
local v1 = cpml.vec3(1, 2, 3)
local v2 = cpml.vec3(3, 2, 1)
print(cpml.vec3.add(v1, v2)) -- (4, 4, 4)
print(v1:add(v2)) -- (4, 4, 4)

Back to TypeScript, what this essentially means is that for me to create my objects, I need to invoke __call as new is a reserved word in TypeScript.

declare module "cpml" {
  export namespace cpml {
    type Vec3 = { x: number, y: number, z: number };
    function vec3(x?: number, y?: number, z?: number): Vec3;
    namespace vec3 {
      function add(a: Vec3, b: Vec3): Vec3;
    }
  }
}

The above definition works, but the caveat is that it is all within the same file. I cannot for the life of me find a way to move that vec3 namespace into another file and then import it into this file without some error. Whether it's a namespace, an interface, its own module, a class.. nothing I do will allow me to stick that in another file. I always end up with the same issue: my vec3 function (which must be named that to invoke __call) complains about a duplicate identifier when i try to import my vec3 namespace from another file.

declare module "cpml" {
  export namespace cpml {
    const Vec3 = typeof import("./vec3.d.ts").Vec3;
    function vec3(x?: number, y?: number, z?: number): Vec3;
    const vec3: typeof import("./vec3.d.ts").vec3; // nope!
  }
}

Any insight into this would be more than appreciated.

0

There are 0 best solutions below