The generated code is unable to import the file compiled by Clasp

I am encountering an issue with my TypeScript files in my Google App Project. Here is a breakdown of my files:

src/main.ts


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

src/other.ts

    console.log('hello world');
}

This setup generates the following in my 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;

However, when I try to run the main() function in main.gs, I encounter an error. Check the error message https://i.sstatic.net/2sCzb.png

How can I resolve this issue and make imports work effectively in my clasp project?

Answer №1

Adjustment Suggestions:

  • Regarding the code snippet

    //import {hello} from "./other";
    and (0, other_1.hello)();, it is worth considering that the issue you are facing might be related to Google Apps Script not supporting the import feature at the moment.

  • Additionally, in the context of Google Apps Script, if we have a function like

    function hello() {console.log("hello world");}
    in a script file named other.gs, it can be directly accessed from another script file such as main.gs.

Based on these observations, how about trying out the following adjustment strategies?

Approach 1:

main.ts

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

other.ts

function hello(): void {
  console.log("hello world");
}
  • With this adjustment, running the main function will execute both console.log("main") in the main function and
    console.log("hello world")
    in the hello function within the Google Apps Script editor.

Approach 2:

This approach involves utilizing Class objects.

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");
  }
}
  • By making this adjustment, executing the main function will trigger both console.log("main") in the main function and
    console.log("hello world")
    in the hello function using the Google Apps Script editor.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to efficiently populate HTML elements with JSON data

I am working on grasping the concept of functional programming. My understanding so far is that it involves encapsulating everything into functions and passing them around. For instance, in my current example, I am attempting to fetch data from a RESTApi a ...

Automatically convert user input to MM/DD/YYYY format in typescript as they type the date

Hello, I am currently working on a React Native app where users input their date using TextInput. As the user types, I would like to automatically format the date as MM/DD/YYYY. Here is the function I have created so far: const formatDate(value: string ...

Creating intricate structures using TypeScript recursively

When working with Angular and TypeScript, we have the power of generics and Compile-goodness to ensure type-safety. However, when using services like HTTP-Service, we only receive parsed JSON instead of specific objects. Below are some generic methods that ...

The user authentication service indicates that no user is currently logged in

Currently, I am working on implementing a route guard to distinguish between authenticated users and guests. In order to achieve this, I have created an auth-guard service along with an auth service. Although the user data is successfully stored in the loc ...

Enhancing class functionality with decorators in TypeScript

Over on the TypeScript's Decorator reference page, there is a code snippet showcasing how to override a constructor with a class decorator: function classDecorator<T extends {new(...args:any[]):{}}>(constructor:T) { return class extends con ...

Assign a class to an Angular component's host element without any conditions

In my Angular project, I have a component where I need to apply a class unconditionally. To achieve this, the host property within the @Component decorator can be used: import { Component } from '@angular/core'; @Component({ selector: 'ap ...

What are some ways to prevent Visual Studio from adding an extra space in JavaScript code during the build or rebuild process?

In my Visual Studio 2019 Enterprise setup, I have noticed that when I build or rebuild my ASP.net 4 MVC solution, my JavaScript files are regenerated by TypeScript. The issue is that the new JavaScript files always end up with a single trailing space after ...

Error message: TypeScript: Attempting to access a value from an enum results in an implicit 'any' error for the element

I'm striving to achieve the most precise solution for this issue. I have a parameter that can be a number or a valid string, and I am utilizing it as an index to retrieve the value of a declared enum. Due to Typescript's lack of knowledge about ...

Checkbox: Customize the appearance of the root element

I am trying to modify the root styles of a Checkbox component, but it is not working as expected. Here is my code: <CheckboxItem onChange={()} checked={isChecked} label="Show Checkb ...

Creating a custom Map type in TypeScript

I am exploring the concept of defining a Map type in Typescript using generics. Essentially, I want to create something similar to: EntityMap<U, V>, where U can only be either a string or a number This is what I have managed to come up with so far: ...

Error in TypeScript: The type 'Ticket[] | undefined' is not recognized as an array type

How can I add a new item to an array of objects in React state using TypeScript? Here is the code snippet in question: export type AppState = { tickets?: Ticket[], } export type Ticket = { id: string, title: string; } export type ApiC = { ...

Is there a way to retrieve the ReturnType of functions based on a parameter list in Typescript?

I am struggling with defining a main function myMainFunction() that accepts a list of functions with different return types as parameters. My goal is to have the return type of myMainFunction be determined by the return types of the functions passed to it ...

Facing an issue with the ng2-carousel component where the left and right arrow icons are

Currently employing ng2-carouselamos Visit the ng2-carouselamos npm page here After duplicating the provided HTML template, I have encountered issues with clickable events not functioning properly. The absence of a TypeScript file in the documentation ma ...

Does Typescript not provide typecasting for webviews?

Typescript in my project does not recognize webviews. An example is: const webview = <webview> document.getElementById("foo"); An error is thrown saying "cannot find name 'webview'". How can I fix this issue? It works fine with just javas ...

Encountering an error when trying to access this.$store.state in a basic Vuex

Encountered an error with return this.$store.state.counter: The property '$store' does not exist on the type 'CreateComponentPublicInstance<{}, {}, {}, { counter(): any; }, {}, ComponentOptionsMixin, ComponentOptionsMixin, EmitsOptions, ...

Obtain the ViewContainerRef object from the Component

Looking to create nested components in Angular 4? This is the Chooser Component import {InputComponent} from './input/input.component' import {BlockComponent} from './block/block.component' export const FormChooser = { Block: Block ...

Effortlessly bring in Typescript namespace from specific namespace/type NPM package within a mono-repository

Instead of repeatedly copying Typescript types from one project to another, I have created a private NPM package with all the shared types under a Typescript namespace. Each project installs this NPM package if it uses the shared types. index.d.ts export ...

Creating encoded objects for a content-type of `application/x-www-form-urlencoded`

Upgrading AngularJS Code to Angular 2+ - Http Issue I am in the process of converting some legacy AngularJS code (specifically Ionic 1) to the latest version of Angular (Ionic 4), and I've encountered a troubling issue. Previously, in every HTTP POS ...

NGRX refresh does not result in any successful actions

Having an issue with loading users into a mat-selection-list within a form. Everything works fine the first time, but upon page refresh, the selector returns 'undefined'. Initially, both GET_USERS and GET_USERS_SUCCESS are triggered (console log ...

Rendering a child component in the DOM tree with Aurelia StageComponent

Imagine having an app.html template structured like this: <template> <require from="./MyComponent"></require> <h1>${message}</h1> <my-component>test</my-component> </template> Accompanied by MyCompone ...