Issues with converting Typescript to ECMAScript 5 causing errors with Custom Elements v1 in IE11

I've encountered a challenge trying to make a basic web component function properly when transpiled to ES5. It works seamlessly on Chrome, Edge, and Firefox, but IE11 is throwing an error in the constructor of the component stating "The custom element constructor did not produce the element being upgraded."

UPDATE

The insightful analysis by Oliver Krull below has effectively identified Typescript's compiler output as the root cause of the issue. Is there a way to resolve this?

The initial source code (in TypeScript):

import "./AppDrawer.less"

class AppDrawer extends HTMLElement {
    get open() {
...

<p>The HTML:</p>

<pre><code><!doctype html>
<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.0.0-rc.8/webcomponents-lite.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/1.0.0-rc.8/custom-elements-es5-adapter.js"></script>
        <script src="bundle.js"></script>
    </head>
   ...

Answer №1

To ensure compatibility with custom elements v1 in all web browsers, including Internet Explorer 11, we must incorporate the native-shim found here. More information on this can be found here.

While this solution works seamlessly in major browsers such as Edge, Safari, and Firefox, it encounters issues in IE11.

The problem arises from syntax errors due to es6 code used in the shim when imported into ie11.

A quick fix is to compile the shim to es5 and include both es6 and es5 versions (es6 first, then es5) to avoid errors.

Though not the most elegant solution, it effectively resolves the compatibility issue.

I have provided a repository with a functional example (notably absent the disabled attribute due to conflicts).

Once again, while it may not be perfect, it gets the job done ;-)

Answer №2

The problem arose from the fact that Typescript's ES5 class implementation is not compatible with the custom-elements-v1 shim. To resolve this issue, one can configure TypeScript to produce ES6 code and then run it through Babel for transpilation.

let rollupTs = rollupTypescript(require('./tsconfig.json').compilerOptions);

rollup({
    entry: "src/client/main.ts",
    plugins: [
        rollupTs,
        less({insert: true}),
        resolve({module: true, jsnext: true, main: true}),
        babel({
            presets: [
                [es2015, { modules: false } ]
            ],
            plugins: ["external-helpers"],
            exclude: "node_modules/**"})
    ],
    dest: "bundle.js",
    format: "iife"
})

However, as mentioned in the accepted answer, adding the es5 transpiled shim to the typescript generated code resolves the compatibility issues without affecting the output of Babel compilation. This solution was accepted due to its simplicity as a quick fix, even though the reason behind its effectiveness is not fully understood.

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

Methods for defining a variable in React with Typescript without encountering the "Index signature is missing in type" issue

I am facing an issue while trying to declare a variable 'reports' and assigning it to an array of arrays containing string and number types. The problem arises when I try to assign it, resulting in errors. ... // Retrieving data from an API let ...

Ways to incorporate Bootstrap components into an Angular application without relying on external modules

I'm attempting to activate Bootstrap 5.3 Tooltips within my Angular 17 application. Within angular.json, I've added bootstrap.bundle.min.js, which includes PopperJS. "scripts": [ "node_modules/bootstrap/dist/js/bootstrap.bundle. ...

Changing a field in a Firestore document to a boolean results in the type being converted to a

I manually created this Firestore document using the console, setting enabled as a boolean type. Now, I am expanding on the functionality to update it in Angular with the following code snippet. activateMenuItem(venueId: string, menuId: string, menuItem ...

Error in Typescript: Function expects two different types as parameters, but one of the types does not have the specified property

There's a function in my code that accepts two types as parameters. handleDragging(e: CustomEvent<SelectionHandleDragEventType | GridHandleDragEventType>) { e.stopPropagation(); const newValue = this.computeValuesFromPosition(e.detail.x ...

Expanding index signature in an interface

Is there a way to redefine the old index signature when trying to extend an interface with different property types? I am encountering an error when adding new properties that have a different type from the original index signature. interface A { a: num ...

How to format a date in ngModel using Angular 9 and above

Is there a way to format the date displayed in my input using ngModel to follow a specific format like 'MM/dd/YYYY'? Can this be achieved by using ngModel? For example, could we do something like model[(ngModel)]="data.targetDate | date:'MM ...

Converting JSON responses from Observables to Arrays of objects in Angular

I have created a custom interface called Table which defines the structure of my data. export interface Table { id: number; title: string; status: string; level: string; description: string; } In my service, I am using HttpClient to se ...

Is there a way to access my JavaScript library from Angular?

How do I incorporate my JavaScript library into Angular? I have a JavaScript file named eletronic-prescription.js function EletronicPrescriptionReport(data) { var wrapper; var medicineIndex; var itemsPerPage = 3; var prescriptionDate; ...

Guide for validating three forms with a single button click in Angular 6 using TypeScript (TS)

Currently, I am retrieving data from 3 separate forms and storing it in 3 different services/tables. I am now looking to validate the data from these 3 forms. In cases where a required field is left empty by the user, an error message should be displayed. ...

Issue - firestore has not been defined (Occurs strictly after the use of "then")

Having an issue: I successfully create fake users in my database, but encounter a problem when starting the 'for' loop. The error I'm facing is: Error adding document: TypeError: Cannot read property 'firestore' of undefined I ...

Utilize ngx-translate in Ionic 2 for translating menu items

I have successfully implemented ngx-translate for multi-language support in my application. However, I am now looking to extend this functionality to my menu items. How can I achieve this for my 3 menu items with different titles? ts file appPages: Pag ...

Using TypeScript and Node.js with Express; I encountered an issue where it was not possible to set a class property of a controller using

I have a Node application using Express that incorporates TypeScript with Babel. Recently, I attempted to create a UserController which includes a private property called _user: User and initialize it within the class constructor. However, every time I ru ...

Exploring the concept of nested arrays in Angular 2 using Typescript

I'm exploring the possibility of defining an array in Angular 2 that contains multiple other arrays within it. To clarify, here's what I had initially: export class PaymentDetails { account: any[]; bpNumber: number; } The issue with t ...

Angular 4+ directive allowing interaction with the NgModel of a component

I'm looking to update styles based on the state of NgModel.control. To keep it DRY, I was thinking that a directive for reading the NgModel component state could be the solution. Is this actually feasible? I haven't been able to find any guidanc ...

Troubleshooting problem with TypeScript and finding/filtering operations

let access = environment.access.find(it => it.roleName == userRole); Property 'filter' does not exist on type '{ siteadmin: string[]; manager: string[]; employee: string[]; contractor: any[]; }'. This scenario should work perfectly ...

Varieties of data classifications for the information obtained from supabase

1 I'm encountering an issue while attempting to define the data types from Supabase. I received an error message stating: "type '{ id: string; title: string; capacity: number | null; start_date: Date | null; start_time: string | null; end_ ...

Pause the execution at specific points within Typescript files by utilizing breakpoints when debugging with an attached debugger in VsCode

My current challenge involves setting up a debugger in VsCode with the attach mode on a Typescript codebase running in a Docker container. Despite successfully attaching the debugger in VsCode and hitting breakpoints, I consistently find myself landing on ...

Using Angular 4's ngComponentOutlet to showcase ContentChildren that are dynamically changing

My main objective is to create a unique container component with a dynamic list of customized card components that can be accessed individually (instead of as a group using ng-content). <custom-card-holder> <custom-card></custom-card> ...

Inversify is a proven method for effectively injecting dependencies into a multitude of domain classes

Struggling to navigate dependencies and injections in a TypeScript-built rest web service without relying heavily on inversify for my domain classes, in line with the dependency inversion principle. Here's an overview of the project structure: core/ ...

How can I extract fields from a response using Axios?

I came across this code snippet: axios.get<Response>(url) .then(response => { console.log(response.data); }).catch(error => { // handle error console.log(error); }); The JSON response I am receiving has fields that are not easily e ...