How to Make Sure Decorator Metadata in Typescript and Angular 2 Points to Variables in Scope to Prevent "Not Defined" Errors

While working on my web app, I encountered a crash issue in Safari due to the type definition for a function referring to the TouchEvent interface. This caused problems because the variable was directly emitted in decorator metadata in the compiled JS code and is not supported on desktop Safari (see more here).

Solving this particular instance was straightforward by using Event instead of TouchEvent. However, it got me thinking... Are there potentially other decorator metadata in my application that could be causing issues on untested browsers?

Below is an Angular 2 directive example that demonstrates the issue:

import { Directive, HostListener } from '@angular/core';

@Directive({
  selector: '[touchTest]'
})
export class TouchTest {
    constructor() { }

    @HostListener('touchend', ['$event']) onTouchend(event: TouchEvent) {
        console.log(event);
    }
}

If you compile this with

tsc touch-test.ts --emitDecoratorMetadata --experimentalDecorators
, the generated output includes the following (full JS output can be viewed here):

__decorate([
    core_1.HostListener('touchend', ['$event']),
    __metadata("design:type", Function),
    __metadata("design:paramtypes", [TouchEvent]),
    __metadata("design:returntype", void 0)
], TouchTest.prototype, "onTouchend");

The use of TouchEvent in the code throws an error of TouchEvent is not defined on Safari.

I have observed that some parameter types, especially the ones explicitly imported, are emitted as Object in the JS output, which is considered safe. Additionally, the interface TouchEvent is defined in various .d.ts files within the typescript npm module, allowing me to use it without compile-time errors.

Is there a general guideline or rule I should follow to prevent such problems? It's possible that there might be a problematic setting in my development environment (I initially used angular2-webpack-starter), but after reviewing the tsconfig.json and other relevant configuration files, nothing immediately stood out to me.

Answer №1

The files lib.d.ts prioritize the latest web specifications and may not be compatible with older browsers. This is intentional, as it is your responsibility to verify if a particular built-in class or object is supported by the browsers you wish to target. If not supported, options include using polyfills, finding alternative solutions, or dropping support for outdated browsers (I recommend the latter :) There's no need to cling to the past or accommodate browsers that resist conforming to current standards)

Answer №2

You could also consider implementing the hammer.js library.

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

The most efficient method for personalizing the production baseUrl is using the angular2-webpack-starter

Currently, I am utilizing angular2-webpack-starter version 5.4.1. If you want to find out more about it, you can visit their GitHub page here. In order to adjust the baseUrl for the production build, I found myself repeating the same steps that were outli ...

Switching from MOCK API to real API in Angular: The step-by-step guide

I am a beginner in Angular and I am currently working on connecting a sample app to my API gateway called ContactApp. Right now, it is functioning with a mock API but I want to switch to using a real API server. I followed all the steps from a tutorial on ...

Conditions are in an angular type provider with AOT

I am facing an issue with my Angular project that is compiled using AOT. I am trying to dynamically register a ClassProvider based on certain configurations. The simplified code snippet I am currently using is below: const isMock = Math.random() > 0.5; ...

The Angular project encounters a failure when attempting to run 'ng serve,' resulting in an emitted 'error' event on the worker

Resolved Issue - Update: It appears that the problem was due to an error in my CSS code: Previous: .title & .smaller { color: $dark-blue; font-family: "Roboto"; font-size: 20px; font-weight: 600; width: fit-content; margin: 0; ...

An issue has occurred: TransformError SyntaxError: Unexpected keyword 'const' was encountered

While practicing programming with React-Native, I encountered a problem that I couldn't figure out how to solve. I attempted to use solutions from various forums, but none of them worked. import { StyleSheet, Text, View, Image } from 'react-nativ ...

Tips for updating the styleurls link dynamically by clicking a button and maintaining the changes

In the midst of developing an angular 2 project, I am currently working on integrating theme settings. Within my project, I have crafted three distinct component.scss files: red.component.scss, yellow.component.scss, and blue.component.scss. My goal is t ...

How to resolve the issue of any data type in Material UI's Textfield

I am seeking clarity on how to resolve the TypeScript error indicating that an element has an 'any' type, and how I can determine the appropriate type to address my issue. Below is a snippet of my code: import { MenuItem, TextField } from '@ ...

Having trouble showing the specific date using the Angular datepipe

Working with angular 7 at the moment. Upon using the date pipe, I have encountered an issue where it displays the next date instead of the current one. Here's a snippet of the code: <div style="text-align: left;width: 80px;white-space:nowrap"> ...

Troublesome situation encountered when trying to implement React Native/Xcode Release using Typescript

Currently facing an issue while trying to generate a release version of my RN/Typescript project for iOS. I have made some changes to the "Bundle React Native code and images" as follows: export NODE_BINARY=node ../node_modules/react-native/scripts/re ...

Utilize Polymer 3 component to import object values

In one of my polymer 3 components, I have declared an object property as follows: static get properties(): myInferface { return { myObject: { type: Object, notify: true, value: { showMe: false, text: ...

Is there a way to keep chart values from disappearing on the edges of the x-axis in Highcharts when zooming in?

Currently, I am working with highcharts-react-official to create a histogram using typescript. However, I have encountered an issue where the values on the edges of the x-axis disappear when users zoom in (highlighting relevant x-axis values). View Example ...

Exploring the Concepts of Angular 2, Material 2 Radio Buttons, and Formgroups

Struggling to integrate Angular 2 and Material 2, specifically with a FormGroup and an <md-radio> component. Despite following the usual setup method used for <md-input>, an error is thrown. Here's an example: component.html <form [fo ...

Eliminating null values from a multidimensional array

Is there a way to remove the array elements cctype, cctypologycode, and amount if they are empty? What would be the most efficient approach? { "ccInput": [ { "designSummaryId": 6, "CCType": "A", "CCTypologyCode": "A", "Amount ...

Using Angular service within InnerHTML functions

Within my Angular 10 application, I am utilizing innerHtml to display some content that includes anchor links. My goal is to trigger a function every time a link is clicked, which will then invoke an Angular service. In the code snippet below, I am attac ...

Scaling the ion-spinner to fit your specific needs

In my Ionic application, I am utilizing the ion-spinner. However, I have encountered an issue with resizing the spinner. While custom CSS works well with default spinners, it does not function properly with Bubbles, Circles, and Dots spinners. CSS .spin ...

Angular retrieve - nested mapping

Having trouble with calling an API method inside a map function, as the second API call is not being made without any console errors appearing. //The following method is not getting called - The API method is not being triggered this.getUserPermissions(lo ...

The seamless integration of NestJs, Angular, and Cognito for a streamlined user

Implementing the Authentication and Authorization flows can be tricky without concrete examples. While NestJs doc provides a good example for JWT tokens, I still struggle to establish the correct flow. Here is my current approach to solving the authorizat ...

Transforming the elements within an array of objects into individual key-value pairs and then adding them to a fresh array

Hello, I am diving into the world of Typescript/Javascript and encountering some challenges when it comes to manipulating arrays of data. Please bear with me as I navigate through my learning curve. Imagine having an array of objects structured like this: ...

Create a function that can accept either no parameters or one parameter depending on the input parameter provided

Do you think there is a more efficient way to write this code? The function Z can be called with either one parameter or with no parameters. If the parameter Y is passed in, then the function z(y) is returned, otherwise just run the function z() async x ...

Issue with Angular 5 Application - "Implementations cannot be declared in ambient contexts"

Recently in my Angular 5 project, I started encountering an issue with the observable object. Everything was working smoothly until about a week ago when I began receiving this error message: ERROR in node_modules/rxjs/Observable.d.ts(20,31): error TS1183 ...