Search code examples
node.jstypescriptdefinitelytypedbabylonjs

Use types from one module for another


I'm using Babylonjs with the Rollupjs together with typescript.

babylon.d.ts

Babylonjs has typings bundled with the package so when I import it like this:

import { ArcRotateCamera, Engine, SceneLoader, Vector3 } from "babylonjs";

I get all of the typing information and type-checking from typescript, however, due to (I think) how babylon.js is bundled - Rollup cannot bundle it throwing errors:

1: import { ArcRotateCamera, Engine, SceneLoader, Vector3 } from "babylonjs";

2: import "babylonjs-loaders";

3: import React from "react";

Error: 'Engine' is not exported by node_modules/babylonjs/babylon.js

To fix rollup bundling I need to change imports to:

import { ArcRotateCamera, Engine, SceneLoader, Vector3 } from "babylonjs/es6";

but then I lose all of the typing information...

So my quickest solution to that would be to somehow instruct typescript that babylon.d.ts applies also to babylonjs/es6.js.

P.S these named import are all correct and working because component works when I use it with @storybook (which loads it with webpack).


Solution

  • You'll notice how the babylonjs module is declared in babylon.d.ts:

    declare module 'babylonjs' { 
        export = BABYLON; 
    }
    declare module BABYLON {
        // Many declarations...
    }
    

    To associate the same declarations with the babylonjs/es6 module name, all you have to do is follow the same format and add a module declaration to a new file in your project:

    declare module 'babylonjs/es6' { 
        export = BABYLON; 
    }
    

    (Adding this code inside another module file will not work because then it will be treated as a module augmentation instead of a module declaration.)

    Consider filing an issue to have this change made in the original babylonjs package.