Developing an attribute in a constructor using Typescript

My javascript code is functioning perfectly:

class myController {
   constructor () {
      this.language = 'english'
   }
}

However, I encountered an issue when trying to replicate the same in Typescript

export default class myController {
  constructor () {
    this.language = 'english'
  }
}

This resulted in the following error:

Property 'language' does not exist on type 'myController'.ts(2339)

Why does this error occur when attempting to create a property for myController?
What would be the correct approach to resolve this issue?

Answer №1

It seems that the error is occurring because you are attempting to assign a value to a property that has not been declared in the class. Make sure to declare the property before setting its value, like this:

export default class myController {
  private language: string;
  constructor () {
    this.language = 'english'
  }
}

Answer №2

To properly define it, you must declare it as a Property on the type (usually done before the constructor method):

export default class myController {
  preferedLanguage: string;
  constructor () {
    this.preferedLanguage = 'english'
  }
}

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 radio button is not appearing correctly in my HTML form

Seeking guidance as a newcomer to html and css. Looking to create a signup form using html and css, but encountering issues with the radio button not displaying in the browser. Unsure of the root cause of the problem. Any assistance would be greatly apprec ...

Is there a noticeable distinction or significant benefit between using auth.signOut() and signOut(auth)?

When it comes to logging out with Firebase, is there a significant difference or advantage between using auth.signOut() or signOut(auth)? This concept also applies to other authentication processes like logging in and signing up. The Firebase documentation ...

What does an exclamation mark signify in Angular / Type Script?

Being a newcomer in the world of front end development, I am currently teaching myself Angular (11.2.10). While exploring a sample project, I noticed this particular piece of html template code written by another person and utilized in multiple places: < ...

What function is being executed in this specific scenario?

This excerpt is taken from the book "Node.js Design Patterns - Third edition: Design and implement production-grade Node.js applications using" function patchCalculator (calculator) { // new method calculator.add = function () { const addend2 = cal ...

A guide on testing mouse clientY in React using JEST for effective testing

useEffect(() => { const mouseHandler = (event: MouseEvent) => { menuData.forEach((element) => { if (element.hasActiveDropdown && event.clientY > 50) { handleCloseDropDown(); // handleDropDown('0') ...

I possess a single input field and I am seeking to have the focus shifted to it upon the activation of a button

Looking to enhance user input with an icon interaction: Clicking the icon should focus on the input Considering a solution for when clicking the icon to trigger focusout A code snippet has been implemented for the first requirement, seeking suggestions ...

Issues with Ionic 3 Directive Not Functioning

Struggling to create a custom directive in Ionic that won't resize automatically? I can't figure out what's going wrong. Here's the code snippet from my project, which is an Ionic 3 app with Angular 4: import { Directive, HostListener ...

Adding the <br> tag in a Bootstrap modal: a simple guide

Hi everyone! I am currently working with a bootstrap modal to create a login functionality within the modal itself. There is a data validation function in place. However, when an error occurs, the word 'br' displays directly in the HTML instead ...

What is the best way to display these images correctly within a slider?

My goal is to display 4 images/company logos per slide, but they all end up clustered on one slide. Despite my efforts to adjust the CSS, nothing seems to work. This is what my code currently renders: https://i.sstatic.net/ThaOK.png Here are the react co ...

Experiencing issues with the Google Drive file selection API due to JavaScript

I am having trouble implementing a Google file picker in my Django Project. Every time I try to create an HTML page with an integrated Google file picker, I encounter a 500 "Something went wrong" error. https://i.sstatic.net/6JMh7.png This issue arises a ...

Spin an object around the global axis using the tween.js library

I'm trying to achieve a gradual rotation of a cube on the world axis using tween. Initially, I was able to rotate the cube around the world axis without tweening with the following code: rotateAroundWorldAxis(cube[1], new THREE.Vector3(0,1,0),degreeT ...

Choosing specific information from a deeply nested JSON structure

I am currently dealing with a multi-level JSON object that includes an array of 143 other objects. When I run console.log(obj) on this object, the following output is displayed: 0: Object ActFTEs: 0.00 Actual: 11111 Bud_Month: "October" FY_CD: 201 ...

Retrieve the fully loaded webpage source code in Java as it is displayed by the browser

There are certain webpages that utilize JavaScript and AJAX calls to populate fields during or after the page has loaded. An example can be found at this link, where the content in a size dropdown box is filled using JavaScript. I am wondering if it is po ...

Returning to the location in the file where the redirect function was invoked in the Express framework

In my current project, I am working on an Express app and facing a challenge. I need to redirect after collecting data in a function, complete the redirect, return the data, and then resume from where I left off. For instance: > res.redirect('my/ ...

Retrieve all the records from the collection that have a specific reference number in their ID field

Is it feasible to pull together all documents with an ID that includes a specific value? I am working with Angular 7. I attempted using db.collection('items').where.. but unfortunately, this method is not supported. For instance: (collection) ...

Retrieve data from a form on the server side using an Ajax request

I am currently working on submitting form data through an Ajax call. Here is the relevant form code: <form target="_blank" id="addCaseForm" class="form-horizontal col-md-12" action="" method="POST> <div class="form-group"> <labe ...

Repairing the 'Uncaught TypeError: Cannot read property 'split' of undefined' bug in a Prestashop Module

Help! I'm encountering an issue with a Prestashop module, and the developer is unresponsive. Can anyone shed light on why I am seeing this error in the console? Thank you so much in advance! admin.js:57 Uncaught TypeError: Cannot read property ' ...

Having trouble with my jQuery .hover() code not running as expected

Whenever I hover over my divs, I want them to change color. However, the code doesn't seem to be working as expected when I try to do so. I suspect that the issue might be related to the z-index property used in the class that I am trying to hover ove ...

"Uncovering a memory leakage issue within a recursive polling function in JavaScript

Current issue in need of resolution: My team and I are currently working on a large application that was initially released around two years ago. We are in the process of adding a new "always-on" status screen to the application, which will be the default ...

Using javascript to locate and substitute a word divided among multiple tags - a step-by-step guide

I need to utilize JavaScript to locate and substitute a word that has been separated into multiple tags. For instance, consider the following HTML code: <html> <body> <div id="page-container"> This is an apple. ...