What is the best way to import a TypeScript file into another script in order to enhance efficiency?

I am currently working on a TypeScript file that captures DOM events and sends data streams to AWS Kinesis. In my HTML, I have the following script for testing purposes:

<script type="text/javascript" src="./tracker.ts?projectId=123&apiKey=trac3ab1e-50fa-50b5-dab5-acce55e5cafe"></script>

Using Parcel, I created a bundle so I'm not directly attaching the ts file in the HTML.

After running npm run dev, the <script> tag changes to:

<script type="text/javascript" src="./tracker.568463.js?projectId=123&apiKey=trac3ab1e-50fa-50b5-dab5-acce55e5cafe"></script>

The tracker.ts contains a class Tracker which loads AWS SDK and sends data streams to Kinesis.

I now want to load this tracker.ts from another script, but I have some questions:

  • How can this be implemented?
  • How feasible would it be?
  • What impact will it have on performance?

When attempting implementation, I tried a couple of things:

Firstly, I changed the

type="application/javascript"
in the <script> tag to type="module", based on this reference. However, this did not work as expected and some parts of the code were not properly read.

Secondly, I looked into importing the Tracker class from tracker.ts as a module, referring to this source. But I am unsure about how to implement it in my specific case, and I also received a warning stating that tracker.ts is not recognized as a module.

Answer №1

It seems like you may be a bit confused. To start, loading TypeScript files directly is not possible. You must first compile the TypeScript code into JavaScript for the browser to understand. Also, attempting to load tracker.ts with parameters (?projectId=123&apiKey=trac3ab1e-50fa-50b5-dab5-acce55e5cafe) is incorrect. This method is only suitable for external resources available on the internet. It is advisable to grasp the basics before delving into complex tasks like AWS Kinetic.

Compiling TypeScript

To compile in the terminal:

npm install tsc tsc tracker.ts

This will generate a tracker.js file that you can then load.

Regarding the Parameters:

Avoid using params and instead add this script before loading tracker.js:

<script>
window.projectId = 'something'
window.apiKey = 'something'
</script>

In the tracker file, you can access these values as follows:

const projectId = window.projectId;
const apiKey = window.apiKey;

Note: After making changes to tracker.ts, re-run tsc tracker.ts to update tracker.js accordingly.

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

Attempting to incorporate a code snippet into a Angular 4 application

Is it possible to embed a gist in Angular 4 using a script tag like this: <script src="https://gist.github.com/Slugpotato/bce9877d9c38d7415960e18fadc4efd7.js"> </script> I am creating tutorials and would like to avoid cluttering the index.ht ...

Experiencing 429 Too Many Requests error on Angular 7 while attempting to perform multiple file uploads

Whenever I attempt to upload a large number of files simultaneously, I encounter an issue. The API interface only allows for the submission of one file at a time, requiring me to call the service for each individual file. Currently, my code looks like thi ...

Saving User-Token securely in a NativeScript application

In my {N}-App, I am looking to create a login system for users. After a successful login, the user receives a token required for making requests. Currently, I store this token using the application-settings module. Now, I want the UI to dynamically switch ...

`Getting Started with TypeScript in an ASP.Net MVC Application`

Due to certain reasons, we have decided to begin our project with TS rather than JS. We are facing issues with the variables set in the MVC Views, which are established by the Model of each View. For example, tes.cshtml: @model Testmodel <script> ...

"Enhance Your Search with PrimeNG's AutoComplete

Currently, I am attempting to implement the autocomplete feature from primeNg based on their documentation, but I am facing issues with displaying the suggestions. Firstly, I added the AutoComplete module by importing it: import { AutoCompleteModule } fro ...

The value of Component variable is not defined

I am facing an issue with my User variable being undefined after calling my service. Here is the code snippet : import { User } from './user/user'; import { AppService } from './app.service'; import { Component, OnInit } from '@an ...

Testing Angular Singleton Service with Unit Tests

I have been tackling a unit test in Angular related to a singleton service. Specifically, I have a UserService that is declared as a singleton in the root module of the application. My goal is to create a unit test that verifies whether the instance of U ...

Intermittent issue with Angular 2 encountered while following the Hero Editor tutorial on angular.io

I am encountering an occasional error in the console while following the angular.io tutorial using Mozilla Firefox. The error does not seem to impact the functionality or rendering of my application, and it only happens sporadically. If you could provide ...

After installing Angular 6, an error is showing up in the file node_modules/rxjs/internal/types.d.ts at line 81, column 44. It is indicating that a semicolon is

I encountered an issue stating: node_modules/rxjs/internal/types.d.ts(81,44): error TS1005: ';' expected. right after I installed Angular 6. Please see the error details below: ERROR in node_modules/rxjs/internal/types.d.ts(81,44): error TS ...

How can you debug a Node.js CLI tool using WebStorm?

Struggling to develop a CLI tool using TypeScript within WebStorm as my IDE. No matter what I try, debugging just won't work for me. My journey in Node.js CLI programming started with this tutorial. Successfully transpiling the TS source with npx tsc, ...

Utilizing unauthorized fetch methods to modify the urql Client's fetch function contradicts TypeScript requirements

I've been struggling to make Typescript happy while redefining the fetch function on @urql/core. Although I came across two helpful solutions on Stack Overflow that seemed to address the issue, unfortunately they didn't quite work for me: fetch ...

The letter L has not been defined in this particular instance (Leaflet.js node package)

I have been attempting to utilize the example provided in the Leaflet quickstart guide within Angular 7. However, I keep encountering the error message ERROR ReferenceError: L is not defined. It is worth noting that I have not included Leaflet via JS files ...

Exploring the potential of AssemblyScript in creating immersive WebXR

I have been exploring three.js and webXR for some time now, and I wanted to incorporate it into assembly script. While I know how to make webXR work in TypeScript, I encounter an error when trying to use it in assembly script with the import statement. Her ...

Error in typography - createStyles - 'Style<Theme, StyleProps, "root"

I'm encountering an error in a small React app. Here is a screenshot of the issue: https://i.sstatic.net/ilXOT.png The project uses "@material-ui/core": "4.11.3". In the codebase, there is a component named Text.tsx with its corresponding styles defi ...

Display the values from form fields in Angular2 after dynamically adding them

I am struggling to output the values of each object in the choices array using console log. Despite being able to display the objects in the choices array, all the values appear empty. Every object is showing as timeZonePicker: "", startTimeInput: "", endT ...

The specified JSX element does no possess any constructors or callable signatures

The root element on the right side of my page is a simple React element that I am currently using. Can you help me troubleshoot and fix the error that is being displayed? https://i.sstatic.net/xdDyn.png ...

Can the GitHub URL be utilized for installing TypeScript npm dependencies?

When working with an npm library written in TypeScript, the usual process involves writing the source code in TypeScript, pushing it to GitHub, then converting it to JavaScript and pushing the resulting JavaScript code to the npm repository. When adding ...

Creating an array of reusable components in Vue 3 with Typescript - How can I pass it into another component?

I have developed a customizable Button component that can be styled dynamically and accept values for text/icons, etc. Here is the code for my "ActionButton" in Vue 3. Although I am new to this framework, I am facing some difficulties understanding certai ...

How can Lazy<T> be integrated into TypeScript?

While working in .NET, I came across the Lazy<T> type which proved to be very helpful for tasks like lazy loading and caching. However, when it comes to TypeScript, I couldn't find an equivalent solution so I decided to create my own. export in ...

Angular2+ test case indicating lack of NgControl provider

I've been facing an issue while testing an angular component with multiple dependencies. The test case fails with the error: "No Provider for NgControl". Here's the detailed error message: Chrome 66.0.3359 (Mac OS X 10.13.4) configurator compone ...