typescriptgoogle-apps-scriptclasp

clasp compile file not able to import in generated code


I have the following typescript files

src/main.ts


function main(): void {
    console.log('main');
    hello(); 
}

src/other.ts

    console.log('hello world');
}

which generates the following in my Google App Project src/main.gs

var module = module || { exports: exports };
//import {hello} from "./other";
function main() {
    console.log('main');
    (0, other_1.hello)();
}

src/other.gs

var exports = exports || {};
var module = module || { exports: exports };
exports.hello = void 0;
function hello() {
    console.log('hello world');
}
exports.hello = hello;

When I try to run the the main() function in main.gs I get the following error: enter image description here

How can I make imports work in a clasp project?


Solution

  • Modification points:

    From these points, how about the following modification patterns?

    Pattern 1:

    main.ts

    function main(): void {
      console.log("main");
      hello();
    }
    

    other.ts

    function hello(): void {
      console.log("hello world");
    }
    

    Pattern 2:

    In this pattern, the Class object is used.

    main.ts

    import { Sample } from "./other";
    
    function main(): void {
      console.log("main");
      new Sample().hello();
    }
    

    other.ts

    export class Sample {
      public hello(): void {
        console.log("hello world");
      }
    }