Issue encountered while trying to import a .ts file into a .js file

I have some .js files and I want to incorporate certain .ts files into them. However, when I attempt to import these files, I encounter some strange errors.

Here is the import statement in my .js file:

import {AuthComponent} from './auth/auth.component';

This is the content of the .ts file (auth.component.ts):

import {Component, OnInit} from '@angular/core';
import {ActivatedRoute, Params, Router} from '@angular/router';
import {UserService} from '../user/user.service'; // which is also a .ts file

@Component({
  selector: 'app-auth',
  templateUrl: './auth.component.html',
  styleUrls: ['./auth.component.scss']
})

export class AuthComponent implements OnInit {
  loadingConfig: any = {
    fullScreenBackdrop: true,
    backdropBackgroundColour: 'rgba(0,0,0,0.9)',
    primaryColour: '#b8860b'
  };
};

Upon trying to compile the application, I am confronted with the following error message:

Error in ./app/auth/auth.component.ts
 Module parse failed: /data/1/projects/myapp/src/app/auth/auth.component.ts Unexpected character '@' (7:0)
 You may need an appropriate loader to handle this file type.

The issue is pinpointed to line 7 where @Component is located.

In my webpack.config.js file, I made the following addition:

resolve: {
   extensions: ['.ts', '.js','.json']
}

Additionally, I created a tsconfig.json at the root of the app folder containing:

"compilerOptions": {
  "target": "es6",
  "module": "commonjs",
  "allowJs": true,
  "checkJs": true
}

I assume it should be possible to import a .ts file into a .js file, but perhaps there is something that I am overlooking? Thank you all for your help!

Answer №1

It is completely ordinary for Javascript to be unaware of Typescript. The error you are experiencing is likely due to the lack of decorator implementation in JS.

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

What strategies can be used to stop elements from reverting to their original state?

Hey there, I'm currently experimenting with animation and trying to create a cool two-step effect: 1. elements falling down and 2. pulsating on click. However, I've run into an issue where after the clicking action, the elements return to their o ...

When attempting to use JSON.parse on a basic object, I keep encountering an error labeled as "Unexpected token"

I'm facing an issue with the following code snippet: var a = localStorageService.get('selectedQuestionSortOrder'); $scope.selectedQuestionOrderBy = JSON.parse(a); var b = 99; Upon inspecting with a debugger, I observe the following: a - O ...

Struggling to properly implement an "Errors" Object in the state function of a React Login Form Component

The issue arose while I was following a React tutorial. My objective is to develop a basic social media web application using Firebase, React, MaterialUI, and more. I am currently at around the 5:40:00 mark and have successfully resolved all previous pro ...

The basic jQuery script is failing to function properly in the Opera browser

I've encountered an issue with a simple jQuery script I wrote. It seems to work fine on most browsers, except for Opera. Can anyone help me figure out what the problem might be? jQuery(document).ready(function() { jQuery("input:radio[name=virtuem ...

What is the reason behind the improved performance I experience in Chrome with the Developer Console open?

Currently, I am developing a small canvas game completely from scratch using pure JavaScript. This game utilizes a 2D lighting algorithm that is akin to the one found here. It features only one light source and 25 polygons, resulting in approximately 30,0 ...

Unit testing in Typescript often involves the practice of mocking

One challenge with mocking in Typescript arises when dealing with complex objects, as is the case with any strongly-typed language. Sometimes additional elements need to be mocked just to ensure code compilation, such as using AutoFixture in C#. In contras ...

Feeding JSON information into an AngularJS Bootstrap popup

Despite my extensive search on S-O, I couldn't find a solution to my particular issue. If this has been asked before, please excuse me. My goal is to display JSON data in a modal (using UI-Bootstrap), but I'm facing difficulties. Even after tryi ...

Scale down the canvas element before retrieving the image data

In order to feed into an algorithm, I am looking for a way for users to draw on a canvas element and then convert it into an array of length 784. This array should represent the pixel shade intensity, which can be obtained using the ctx.getImageData() meth ...

The onbeforeunload event should not be triggered when the page is refreshed using a clicked button

Incorporated into my webpage is a sign-up form. I would like it to function in a way that if the user accidentally refreshes, closes the tab, or shuts down the browser after entering information, a specific message will appear. <input type="text" place ...

Ways to eliminate redundant older React components within a different library

Having trouble with material-ui in my React project as I encounter this error. Error: Invalid hook call. Ensure hooks are only called inside the body of a function component. Check for: Possible mismatch of React and renderer versions (i.e., React DOM) ...

Is it possible to customize the MUI CSS for a specific menu item using the sx prop?

How can I apply the MenuItemStyle to the sx prop of a MenuItem using: const MenuItemStyle = { '&:hover': { backgroundColor: `${theme.color.secondaryHover}`, }, '&:selected, &:selected:hover': { backgroundColor: ...

Angular Firebase Authentication: Simplifying User Authentication in Angular Apps

Tools I'm Utilizing Angular 5 AngularFire5 Firebase & Firestore Objective I am working on developing a straightforward authentication/login and registration system. While I have already created one, I am facing some challenges and seeking the ...

Data input tag fails to upload

I'm currently working on an app that aims to upload all data from a hard drive into an array. The structure consists of a main folder with subfolders, and my goal is to extract the names of all subfolders and store them in the array. However, I'v ...

I'm puzzled as to why a div tag suddenly becomes invisible when I attempt to link it with a v-for directive in my code

I am facing an issue with the div tag assigned to the log class. My objective is to populate the text using data retrieved from the API response. However, when I attempt to include the v-for directive, the entire div mysteriously disappears from the brow ...

User-triggered event not being emitted back from SocketIO server

In my React frontend, I am using socket.io-client and in the Express backend application, I am utilizing socket.io. There is an event on the server that sends a message to all users in a room when triggered. socket.on('connection', () => { ...

Difficulty resolving the issue of 'source.uri should not be an empty string' in React Native

I'm encountering an issue with resolution source.uri cannot be left blank while working with React Native. I'm puzzled about the origin of this error. My component contains 3 Flatlist that display children components using props from the pa ...

Using Typescript to create a mapped type that allows for making all properties read-only, with the exception of

I encountered a problem where I didn't want to repeatedly rewrite multiple interfaces. My requirement is to have one interface with full writing capabilities, while also having a duplicate of that interface where all fields are set as read-only excep ...

Activate the button in React after ensuring all form fields are populated

Hello everyone, I'm new to using React and I'm trying to implement some basic validation but I'm struggling. Here are my goals: Disable the button if any form fields are empty Enable the button only when ALL form fields are are filled I w ...

Can Google Drive be utilized with JavaScript without having to use Google Cloud Console?

I am exploring ways to utilize Google Drive for sharing data between an Android app and a web page hosted on my server. The webpage is intended to interact with Google Drive using JavaScript for retrieving and storing data. The data transmission should st ...

What are the typical scenarios where Angular's DI resolution modifiers are used?

Recently, I delved into the world of resolution modifiers, but I am struggling to grasp their practical application in my project. It appears that they are mainly used when dealing with services and requiring different instances of them in various parts of ...