What causes my TypeScript to occasionally return an instance and other times return a class?

I've been developing an app to enhance my understanding of TypeScript. It functions perfectly in Chrome, however, I encounter a problem when running it in Edge. The issue lies within this method:

 set position(pos: Point) {
        const diffAsPoint = pos.minus(this.canvasPos);

        let diff: Vector2D = diffAsPoint.toVector2D(); // <--- this line
        if (diff instanceof Vector2D) {
            console.info("is a vector!");
        } else {
            console.info("not a vector!");
        }

Occasionally, I notice that diff IS a Vector2D rather than being an instance of a Vector2D. This leads to errors such as 'Object doesn't support property or method ...'

https://i.sstatic.net/kZPQf.png

The toVector2D method is quite simple:

toVector2D(): Vector2D {
    return new Vector2D(this.x, this.y);
}

In case it matters, here's some context:

  • the app is a game running in a loop (60 frames per second - utilizing

    window.requestAnimationFrame(() => this.animloop());
    )

  • it performs well in Chrome

  • it encounters issues in IE (though with different symptoms, debugging proves challenging as the tools crash)

  • latest TypeScript version (2.2.1) is utilized

  • errors occur randomly after startup, ranging from 2-3 seconds into execution

  • similar occurrences happen elsewhere in the code related to Point and Vector2D, seemingly linked to creating them each frame (some problems are resolved by introducing a field instead of creating one each frame)

  • AMD and requirejs are employed

UPDATE -------

Upon further examination using Edge and debug tools with 'Always refresh from server' enabled, while loading from the website (), it becomes apparent that the game tries to initiate before all modules are fully loaded. In contrast, Chrome appears to wait for complete loading before executing the game.

Any suggestions or insights?

Answer №1

One issue I encountered with the app is that it seems to be using default parameter values in the transpiled code:

  reset(isDemoMode = false) {

This ES6 feature may not be supported by Edge, whereas Chrome likely does support it.

tsc should automatically transpile this to ES5. Make sure your configuration is correct and not set to output ES6 code.

For example:

function obj(x = 5) {  
}

should be transpiled as:

function obj(x) {
    if (x === void 0) { x = 5; }
}

Here is a link to the playground.

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

When using Angular, the Zone can be accessed through VSCode intelligence, but it appears as undefined when running in

Upon attempting to utilize Zone within the ngOnInit function in my Angular application, I placed a breakpoint on line 11. However, when inspecting the function Zone(parent, zoneSpec){....}, it returns undefined upon launching in Chrome browser. Here is the ...

Utilize a node module within a web browser

Currently, I am utilizing the straightforward Refify npm module to manage circular structure JSON. It performs the task of stringifying a circular structure JSON object in Node.js for transmission to the client. Upon receiving the stringified JSON in my An ...

Issue with Loosing Focus in React TextInput when typing the letter "S"

My TextInput is acting strangely - it loses focus only when I type the letter s. All other letters work fine. <FormControl key={"1"} sx={{ m: 1 }} variant="outlined" onChange={handleFilterValueChange}> <InputLabel htmlFor=& ...

Implementing AJAX notifications in a contact form integrated with PHP, Google reCAPTCHA, and SweetAlert

I've implemented an AJAX script to handle the submission of a basic contact form with Google reCAPTCHA at the end of my HTML. Here's the relevant HTML code: <div class="col-md-6"> <form role="form" id="form" action="form.php" method ...

Oops! The system encountered a problem while trying to identify the value `Han` for the property `Script

I'm attempting to extract Chinese characters from a string. According to this particular answer, the following code snippet should work: const nonChinese = /[^\p{Script=Han}]/gimu; const text = "asdP asfasf这些年asfagg 我开源的 几 ...

Dynamically Growing Navigation Bar Elements with Nested Subcategories Based on Class Identification

Let's say you have a menu bar structured as follows: <nav> <ul class="nav"> <li class="menu1"><a href="#">Menu Item 1</a></li> <li class="menu2"><a href="#">Menu Item 2</a> <ul& ...

How can I substitute a specific capture group instead of the entire match using a regular expression?

I'm struggling with the following code snippet: let x = "the *quick* brown fox"; let y = x.replace(/[^\\](\*)(.*)(\*)/g, "<strong>$2</strong>"); console.log(y); This piece of code replaces *quick* with <strong& ...

Utilizing HTML5 data attributes to store intricate JSON objects and manipulate them using JavaScript

Recently, I encountered a unique challenge that I set for myself... I am currently in the process of developing an advanced ajax content loader plugin that comes with an array of options and callbacks. In order to streamline the initialization process and ...

The email input field is not registering the value in the HTML form

There seems to be an issue with the email field in my form. When selecting 'Headteacher' from the drop-down menu, the email input works correctly. However, when choosing 'Primary school teacher', despite entering the details as expected ...

Exploring Multilingual Autocomplete or: Best Practices for Managing Multiple Languages in Web Applications

I'm currently developing a website and I have a mysql-table named 'items' with the following structure: item_id | item (The second column is used to identify the item_id.) In a file called language1.php, I have an array that stores the it ...

Issue with the _.filter function in lodash library when used in a Node.js environment

My goal is to remove rows from a CSV file that already contain email addresses found in Mailchimp. Although I attempted to achieve this with a function, it seems that the number of elements returned is not accurate: async function testDeleteEmails(listID, ...

Utilizing the shared styling feature in a NativeScript app by incorporating it through the angular.json stylePre

Trying to implement Angular 6's stylePreprocessorOptions to easily import shared styling into a component using @import 'app'. My NativeScript project is part of a NxWorkspace setup, with its own angular.json file. Following the instructio ...

Error: Undefined Function [Thinkster.io's Angular Tutorial Chapter 4]

Currently working on the Angular Tutorial provided by Thinkster.io and enjoying every bit of it. However, I've hit a roadblock in Chapter 4 that seems impossible to overcome. Whenever I attempt to execute a Post or Delete action, I encounter the follo ...

Upgrade your development stack from angular 2 with webpack 1 to angular 6 with webpack 4

Recently, I have made the transition from Angular 2 and Webpack 1 to Angular 6 and Webpack 4. However, I am facing challenges finding the best dependencies for this new setup. Does anyone have any suggestions for the best dependencies to use with Angular ...

What's the best way to define the data types for a component that utilizes the set function?

This code snippet seems to be functional, but there are some issues with the types that need to be resolved before it can be compiled successfully. Here is the code in question: function SpotlightElement(props: JSX.IntrinsicElements['spotLight'] ...

When you try to load data in THREE.js and add it to a list, the list does not get populated

When running my data, I utilize the following function: function loadData(){ const loader = THREE.FileLoader(); let loadedData = []; loader.load( correctFileUrl, function (data) { console.log(data); for (let row ...

How can you create a basic slideshow without relying on jQuery to cycle through images?

Imagine you have a div containing 3 images. Is there a way to build a basic slideshow that smoothly transitions between the images, showing each one for 5 seconds before moving on to the next one and eventually looping back to the first image without rely ...

Refining types in a series of statements

I'm attempting to merge a series of assertions in a safe manner without the need to individually call each one. For instance, consider the following code: type Base = { id: string, name?: string, age?: number }; type WithName = { name: string }; type ...

Exploring the bounds of self-invocation functions in JavaScript

Have you ever wondered why self-invocation functions inside another function in JavaScript don't inherit the scope of the outer function? var prop = "global"; var hash = { prop: "hash prop", foo: function(){ console.log(this.prop); ...

What could be causing the ajax request to not go through?

Check out this function I created that triggers an event when any inputs in an HTML form are changed. Function Snippet: function customEvent(form, element) { var timer; $(element).keyup(function () { clearTimeout(timer); if ($(ele ...