Create a variable using a specified class definition

Experimenting with storing data in a model, I have created a class that looks like this:

export class Data {
  constructor(moons: number, color: string) {
    this.moons = moons;
    this.color = color;
  }

  moons: number;
  color: string;
}

I am importing the class using

import { Data } from './data.model';
in my Service. Next, I declare a variable earth of type any.

export class DataService {
  private earth: any;

  constructor() {
    this.earth = new Data(1, 'blue');
  }
}

However, I want to set this variable to the class type Data. When I try to do so with private earth: <Data>;, it throws an error.

The error I receive is:

10:24 - error TS1005: '(' expected.

What is the correct way to assign the class as the type?

Answer №1

Simply utilize

private planet: Info;

The declaration private planet: <Info> is not a valid TypeScript syntax.


Moreover, if you are looking to employ generics, you can use Info<any>. This indicates that the class accepts a generic type and would require it to be defined as follows for private planet: Info<any> to function correctly.

export class Info<T> {

 ...

}

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

Node.js, Sails, and Express aid in examining all parameters with ease

Currently, I am checking my params in the controllers like this: create: function (req, res) { if(req.params.label && req.params.password){ // do stuff } } However, I wish to streamline this process. Is there a tool available that c ...

Is it true that iframe doesn't function as createpopup does?

I decided to use an iframe over window.createPopup, but I'm encountering an error in the console saying "oDocument is not defined". Check out my code below: function ShowDropdown(oMenu, oMenuDropDown){ //Setting up iframe html var iframe='<i ...

What methods can be used to prefill formatted HTML content when launching an email client?

My first step is to generate the formatted html code: onClick={(_) => { const content = `Check out what I found ..<div></div`; Next, I set the content variable window.location.href = `mailto:<a href="/cdn-cgi/l/email-protection" cla ...

Passing data through Angular2 router: a comprehensive guide

I am currently developing a web application with the latest version of Angular (Angular v2.0.0). In my app, I have a sub-navigation and I want to pass data to a sub-page that loads its own component through the router-outlet. According to Angular 2 docume ...

Form Validation behaving differently than anticipated

I have created a custom shopping cart system using PHP and implemented validation with JavaScript, but the validation process is not functioning as expected. Here is a snippet of my PHP script: $result = mysqli_query($conn, "SELECT * FROM products"); whi ...

How can I create a new PHP table using data from an existing table?

I have a table displayed on my website with the code snippet providedview the table image here Here is the code for generating this table: <?php $query = $db->query("SELECT * FROM bit_exchanges ORDER BY id DESC LIMIT 20"); if($query-> ...

Tips to Broaden the Reach of This

Is there a way to expand the reach of this code snippet so that it functions correctly below? I've attempted to utilize the .bind() function without success. var myObject = { message: "Greetings", message2: " Universe", execute: { allF ...

Utilizing Angular Directives and Controllers for Improved Efficiency

I have developed a custom dropdown option selector which is functioning well. It includes functions to fetch data from a specified URL in order to populate a list. However, the issue arises when I attempt to reuse this component in a different section of ...

Sorting rows in ag-grid based on custom data displayed by cellRenderer

My goal is to enable column sorting on cell data that includes custom HTML elements. I understand that I might need to create a custom function to override the default sorting behavior and refer it to the raw values instead of the rendered HTML output. De ...

Retrieving text from a draggable div using jQuery

I have a draggable div that I can move over another element with the class .outerDiv which contains text content. Is there a way for me to retrieve the text from .outerDiv that overlaps with the draggable div? $(".outerDiv .isStore").draggable({ contain ...

Tips for sending FormData and request body with angular's httpclient

I need help updating my Angular application package from @angular/http to @angular/common/http. During the process, my code is breaking for a specific reason. Previously, I was able to send both form data and request body in the following manner: this.ht ...

Traversing within an entity

I came across something confusing to me within the for loop: var info = { "full_name" : "John Doe", "title" : "FrontEnd", "links" : { "blog" : "JohnDoe.com", "facebook" : "http://facebook.com/JohnDoe", "youtube" : "http://www.youtube. ...

Utilizing a jQuery plugin called FullCalendar, dynamically create and set events using a

Using the jQuery full calendar, I am looking to dynamically set events. Here are some examples of events I need to set: events: [ { title: 'Long Event', start: '2016-09-07', ...

Is there a glitch preventing the connection between HTML and CSS in Notepad++ even though the link is correct?

I have encountered a puzzling situation that has left me scratching my head in confusion. Despite being a rookie, I've had experience with linking CSS files before and can't understand why it's not working this time around. This issue is per ...

Formatting dates in jQuery Datepicker

In need of formatting data as yy-mm-dd using jQuery Datepicker and disabling previous dates, I have implemented the following JavaScript. $(function() { $('#edate').datepicker({minDate: 0}); $("#edate").datepicker({ dateFormat: "yy-mm-dd ...

Enforcing strict property validation on variables passed into TypeScript functions

Is there a method to enforce excess-property checking, not only for an inline object literal but also one derived from a variable? For instance, let's say I have an interface and a function interface Animal { speciesName: string legCount: nu ...

Exploring the Angular 7 Sidebar with Bootstrap 4

Is it feasible to design a Bootstrap 4 sidebar with sub menus in Angular 7, similar to the example shown here Appreciate any assistance! ...

Building dynamic 3D scenes using JSONLoader in Three.js and Angular4

Having trouble integrating a JSON file into an Angular 4 environment using a JSONLoader. The path to the JSON file is correct, but I can't add it to the scene. I'm receiving an error "cannot set property 'mesh' of undefined" on the line ...

Organizing a vast TypeScript project: Comparing Modules and Namespaces

As someone relatively new to TypeScript, I am currently working on a small prototyping framework for WebGl. During my project refactoring, I encountered challenges in organizing my code, debating between using modules or namespaces as both have their drawb ...

Developing an angular 6 web client for GRPC

Having experience with grpc using a .net client and a Java grpc server, how can I integrate a grpc web client on Angular 6 using TypeScript? Additionally, what is the process for creating proto files and their typings for TypeScript? I have been referenci ...